Return of c++
-
Upload
yongwei-wu -
Category
Documents
-
view
585 -
download
0
description
Transcript of Return of c++
![Page 1: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/1.jpg)
Return ofC++
![Page 2: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/2.jpg)
About Me
• Software architect in Intel since December 2005• Focused on software design and network security
before joining Intel• ~28 years since writing my first program
– Programming is fun!• A casual open-source contributor
– Several projects, but small contributions– Mostly on multi-lingual support (esp. Chinese)
• A happy father of twins
![Page 3: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/3.jpg)
Programming languages are a fashion world. Languages come, languages go....
![Page 4: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/4.jpg)
Do you know the August ’12 news in TIOBE
Programming Community Index?
![Page 5: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/5.jpg)
C++ dethroned from No. 3 …
again
![Page 6: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/6.jpg)
![Page 7: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/7.jpg)
However, C++ is very much active, having been
active for ~30 years.
![Page 8: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/8.jpg)
Aims of C++
• C++ makes programming more enjoyable for serious programmers.
• C++ is a general-purpose programming language that– is a better C– supports data abstraction– supports object-oriented programming– supports generic programming
![Page 9: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/9.jpg)
Some C++ Design Rules
• C++’s evolution must be driven by real problems.• C++ must be useful now.• Don’t try to force people to use a specific
programming style.• It is more important to allow a useful feature than
to prevent every misuse.• Provide as good support for user-defined types as
for built-in types.• What you don’t use, you don’t pay for (zero
overhead rule).
![Page 10: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/10.jpg)
C++ is biased towardssystems programming,
whyis it popular also in
applications programming?
![Page 11: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/11.jpg)
Reason for C++ in Applications
• Performance of a specialized language
• Multiple categories in a complex application
• Language mix issue• C++ library capabilities
![Page 12: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/12.jpg)
Better C++ Support Coming
• C++11 standardization• C++ native binding in Windows
Runtime• C++ support to mix with Objective-C• Better C++ support in Android NDK• C++11 coming to MSVC, GCC, and
Clang
![Page 13: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/13.jpg)
Why the renaissance?
![Page 14: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/14.jpg)
One word.
![Page 15: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/15.jpg)
Mobile.
![Page 16: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/16.jpg)
More words?
![Page 17: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/17.jpg)
Performance/WPerformance/TPerformance/C
Performance/$
![Page 18: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/18.jpg)
![Page 19: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/19.jpg)
Good timing for the newC++11 standard....
![Page 20: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/20.jpg)
Example
• How short can you get in C++ to dispatch work to a new thread and get the result in the current thread?
• Probably shorter than you expected:future<string> ft(async(...));...
cout << ft.get();
![Page 21: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/21.jpg)
Examplestring flip(string s){ reverse(s.begin(), s.end()); return s;}
int main(){ vector<future<string>> v;
v.push_back(async([] { return flip( " ,olleH"); })); v.push_back(async([] { return flip("\n!letnI"); }));
for (auto& e : v) { cout << e.get(); }}
![Page 22: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/22.jpg)
Examplestring flip(string s){ reverse(s.begin(), s.end()); return s;}
int main(){ vector<future<string>> v;
v.push_back(async([] { return flip( " ,olleH"); })); v.push_back(async([] { return flip("\n!letnI"); }));
for (auto& e : v) { cout << e.get(); }}
![Page 23: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/23.jpg)
Examplestring flip(string s){ reverse(s.begin(), s.end()); return s;}
int main(){ vector<future<string>> v;
v.push_back(async([] { return flip( " ,olleH"); })); v.push_back(async([] { return flip("\n!letnI"); }));
for (auto& e : v) { cout << e.get(); }}
![Page 24: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/24.jpg)
Examplestring flip(string s){ reverse(s.begin(), s.end()); return s;}
int main(){ vector<future<string>> v;
v.push_back(async([] { return flip( " ,olleH"); })); v.push_back(async([] { return flip("\n!letnI"); }));
for (auto& e : v) { cout << e.get(); }}
![Page 25: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/25.jpg)
Compiler Compatibility
• Visual Studio 2012– Pass
• Clang 4.1 & libc++ (OS X)– Pass
• Visual Studio 2010– Range-based for and future are
unsupported• GCC 4.7.2; ICC 13.0.1 (on top of
VS2010)– future is unsupported
![Page 26: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/26.jpg)
Incomplete List of C++11 Features
• Rvalue references and move semantics• Initializer lists• Range-based for-loop• Type inference (auto)• Lambda functions and expressions• Explicit overrides and final• Null pointer constant (nullptr)• Right angle brackets• Unicode characters and strings• Multi-threading memory model• Static assertions• Smart pointers and other standard library improvements• …
Performance
Usability
Functionality
![Page 27: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/27.jpg)
Rvalue References and Move Semantics
• Sorry, it might be too complex for a one-pager…• Problem: copying objects (containers) can be
expensive• Key concepts: object lifecycle, temporary, lvalue,
rvalue• Key result: able to take away the object content in
a reasonable and consistent way, without surprises, via– A new reference type (&&) and related rules– Utilities to convert a type to rvalue reference type (like
std::move)– Move functions to move content to the new object and
erase the original
• Demo of destructor elimination
![Page 28: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/28.jpg)
Initializer Lists
• C++98 allows code like this:int array[] = { 1, 2, 3, 4 };
• C++11 now allows:vector<int> v = { 1, 2, 3, 4 };
• And you can initialize your own container this way:
MyContainer::MyContainer( std::initializer_list<Type>
list){ ...}
![Page 29: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/29.jpg)
Range-based for-loop
• Example:for (int x : { 1, 1, 2, 3, 5 })
{ cout << x << endl;}
• It implicitly calls begin() and end() on the list/array/container.
![Page 30: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/30.jpg)
Type Inference
• Instead of writing:vector<boost::future<string> > ::iterator i = v.begin();
• One can now simply write:auto i = v.begin();
![Page 31: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/31.jpg)
Lambdas
• Convenient where functors can be used– Say, for_each and transform
• Each lambda has a different type– So auto is handy– Class template std::function can be used to
store lambdas• Example:
FILE* fp = fopen(...);ON_SCOPE_EXIT([&]() { fclose(fp); });...
![Page 32: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/32.jpg)
Explicit Overrides and Final
• Examplestruct Base { virtual void some(float); virtual void other();};struct Derived1 : Base { virtual void some(int) override; //
error virtual void other() final;};struct Derived2 : Derived1 { virtual void other(); // error};
![Page 33: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/33.jpg)
Null Pointer Constant
• The definition of NULL causes surprises for:
void foo(char*);void foo(int);...foo(NULL);
• Now foo(nullptr) correctly calls foo(char*).
![Page 34: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/34.jpg)
Right Angle Brackets
• C++98 requires an extra space here:vector<list<string> > sv;
• C++11 allows people to write:vector<list<string>> sv;
![Page 35: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/35.jpg)
Unicode Characters and Strings
• Example:char n[] = "GCC and MSVC encode
differently: \u2018.";wchar_t w[] = L"Is it 16-bit or 32-
bit: \u2018?";
char a[] = u8"UTF-8 string: \u2018.";char16_t b[] = u"UTF-16 string: \
u2018.";char32_t c[] = U"UTF-32 string: \
u2018.";
![Page 36: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/36.jpg)
Multi-Threading Memory Model
• Another complex topic• Memory model is defined• Standard library facilities:
– Mutexes, conditional variables, RAII locks
– Futures and promises (first example)– Atomic operations
![Page 37: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/37.jpg)
Static Assertions
• Example:template<class T>struct Check { static_assert(sizeof(int) <= sizeof(T), "T is not big enough!");};
void DoSomething(...){ static_assert(sizeof(void*) == 4, "Supports only 32-bit platforms"); ...}
![Page 38: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/38.jpg)
Smart Pointers
• Example:unique_ptr<int> p1(new int(5));unique_ptr<int> p2 = p1; // compile errorunique_ptr<int> p3 = move(p1); // transfers
ownership.p3.reset(); // deletes the memory.p1.reset(); // does nothing.
shared_ptr<int> p1(new int(5)); // refcount = 1shared_ptr<int> p2 = p1; // refcount = 2p1.reset(); // refcount = 1p2.reset(); // refcount = 0; frees memory
![Page 39: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/39.jpg)
Summary
• Mobile and cloud make C++ relevant again.
• The new C++11 standard makes C++ a more powerful but easier-to-use language.– Definitely worth adopting – Especially for application developers
• Today is just an introduction.
![Page 40: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/40.jpg)
References• ISO/IEC JTC 1/SC22: N3337 – Draft C++ Standard (very close to C++11)• Bjarne Stroustrup:
Evolving a language in and for the real world: C++ 1991-2006• Bjarne Stroustrup: An Overview of the C++ Programming Language• Scott Meyers: Summary of C++11 Feature Availability in gcc and MSVC• Wikipedia: C++11• 刘未鹏 : C++11 (及现代C++风格)和快速迭代式开发• Stephan T. Lavavej:
Lambdas, auto, and static_assert: C++0x Features in VC10, Part 1• Stephan T. Lavavej: Rvalue References: C++0x Features in VC10, Part 2• Stephan T. Lavavej: decltype: C++0x Features in VC10, Part 3• Thomas Becker: C++ Rvalue References Explained• Herb Sutter: C++ and Beyond 2011: Herb Sutter - Why C++?• Andrei Alexandrescu, Scott Meyers, Herb Sutter:
On Static If, C++11 in 2012, Modern Libraries, and Metaprogramming• Nicolai M. Josuttis: The C++ Standard Library: A Tutorial and Reference (2nd
Edition). Addison-Wesley, 2012
![Page 41: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/41.jpg)
Backup
![Page 42: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/42.jpg)
Poor Man’s Future – Option 1
static string tmp1() { return flip( " ,olleH"); }static string tmp2() { return flip("\n!letnI"); }
int main(){ vector<boost::future<string>> v;
v.push_back(boost::async(tmp1)); v.push_back(boost::async(tmp2));
for (auto i = v.begin(); i != v.end(); ++i) { cout << i->get(); }}
![Page 43: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/43.jpg)
Poor Man’s Future – Option 2
int main(){ vector<boost::future<string>> v;
{ boost::packaged_task<string> pt([] { return flip( " ,olleH"); }); v.push_back(pt.get_future()); boost::thread task(boost::move(pt)); } { boost::packaged_task<string> pt([] { return flip("\n!letnI"); }); v.push_back(pt.get_future()); boost::thread task(boost::move(pt)); }
for (auto i = v.begin(); i != v.end(); ++i) { cout << i->get(); }}
![Page 44: Return of c++](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b6148e4a7959943f8b482f/html5/thumbnails/44.jpg)
Use Boost with MSVC• Most of Boost can be used without building• One needs to add the include path BOOST
– For IDE: Tools > Options > Projects > VC++ Directories > Include files
– For command line: the environment variable INCLUDE• Some components need building, like the unit
test frameworkb2 toolset=msvc-10.0 --with-test debug link=static
runtime-link=static stageb2 toolset=msvc-10.0 --with-chrono --with-date_time --
with-system --with-thread debug release link=static runtime-link=static stage
• One then needs to add the library path BOOST\stage\lib– For IDE: Tools > Options > Projects > VC++ Directories
> Library files– For command line: the environment variable LIB