1 / 49

C++11, VC++11, and Beyond

C++11, VC++11, and Beyond. Herb Sutter. 20 Year Anniversary. C/C++ 7.0 Feb 1992. VC++11 Beta: Feb 2012 ARM targeting Win8 tablet apps C++ AMP Complete C++11 stdlib. C++ AMP. June 2011. C++ , not C mainstream , programmable by millions minimal , just one general language extension

Télécharger la présentation

C++11, VC++11, and Beyond

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++11, VC++11, and Beyond Herb Sutter

  2. 20 Year Anniversary C/C++ 7.0Feb 1992 VC++11Beta: Feb 2012 ARM targeting Win8 tablet apps C++ AMP Complete C++11 stdlib

  3. C++ AMP June 2011 C++,not C mainstream, programmable by millions minimal, just one general language extension portable, mix & match hardware from any vendor, one EXE general and future-proof, designed to cover the full range of hardware heterogeneity – hardware is still in motion open specification Specialized (Y) Processors Specialized (X) Simple InO Complex OoO Memory Unified NUMA cache NUMA RAM Incoherent / weak Disjoint (tight) Disjoint (loose)

  4. C++ AMP Open Specification Today blogs.msdn.com/nativeconcurrency/

  5. VC++ & C++11 You arehere VC10RTM VC11RTM OOBRTM VC11DP VC11Beta OOBCTP Apr 2010 + lambdas, move/&&,auto, decltype,auto fndecls,extern template,nullptr, … Sep2011 + completeC++11 stdlib: thread/mutex,async, future, … + track changes(lambdas, &&, …) Feb2012 + C++11range-for,final,override Out-Of-BandReleases + progressively roll out C++11 initializer lists,template aliases, variadic templates, constexpr, noexcept, =default/delete, … Survey: bit.ly/mscpp11

  6. Roadmap • “C++11 feels like a new language.” • The most pervasive features. • Style, idioms, and guidance. • C++11 adoption scorecard. • How are we doing? • Learning, compilers, and books. • What’s next for ISO C++?

  7. “C++11 Feels Like a New Language” • Great! • But significant change to coding style/idioms/guidance. • That’s why it feels new. Style/idioms/guidance define a language. • Features that significantly change style/idioms/guidance include:

  8. auto

  9. auto • Still static. Simpler. Generic/robust. Handles unutterable types. • Just use auto. Unless you explicitly want to state a type conversion: • Such as to not capture an “ephemeral” type:auto x = make_valarray(1) + make_valarray(2); // oopsauto y = _1 = 1; /* Boost.Lambda */ // oopsauto z = { 42 }; // oops Nothing new here Exactly the same as template arg deduction

  10. Uniform Initialization and initializer_lists { }

  11. Uniform Initialization and initializer_lists

  12. Uniform Initialization and initializer_lists

  13. Uniform Initialization and initializer_lists

  14. Uniform Initialization and initializer_lists

  15. Uniform Initialization and initializer_lists • Use {} unless you have a reason not to. • “Unless” you prefer convenience with simple initialization or auto. int i= 42; // just too ingrained… auto x = begin(v); • “Unless” you need to construct with args that would be a valid initializer_list. vector<int> v( 100 ); // 100 ints (a similar odd case in C++98 got a magic rule)vector<int> v{ 100 }; // one element of value 100 • “Unless” you don’t want auto to capture initializer_list. int n; // Richard Smith’s exampleauto w(n); // intauto x = n; // intauto y { n }; // std::initializer_list<int>auto z = { n }; // std::initializer_list<int>

  16. for, begin, end

  17. for, begin, end • Range-for: Just use it. • Remember auto& if you want to avoid a copy. • Prefer begin(x)/end(x). They’re adaptable to non-STL containers. • Define your own to adapt a container type. (C++11 does this for arrays.)

  18. cbegin, cend • An oversight: find_if( begin(v), end(v), ::: ); // C++11 find_if( cbegin(v), cend(v), ::: ); // not C++11 (yet) • This will be fixed. • For now, write them yourself. template<class T>auto cbegin( const T& t ) -> decltype( t.cbegin() ) { return t.cbegin(); } template<class T>auto cend( const T& t ) -> decltype( t.cend() ) { return t.cend(); }

  19. Smart Pointers standard and portable memory safety

  20. Smart Pointers • Use shared_ptr to express shared ownership, and weak_ptr to express shared observation (e.g., cycle-breaking, optionality). • Use make_shared and gotw102::make_unique. Avoid “new.” • Use a non-owning * or & to observe an object that will outlive you. DELETE

  21. make_unique • An oversight: auto p1 = make_shared<widget>(); // C++11 auto p2 = make_unique<widget>(); // not C++11 (yet) • This will be fixed. • For now, write it yourself. template<typename T, typename ...Args> std::unique_ptr<T> make_unique( Args&& ...args ) { return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) ); }

  22. Parameters: “Pass Expensive Objects By &” • Default: Agnostic to object’s lifetime policy. void f( widget* w );void f( widget& w ); // or && • When you want to commit to shared_ptr lifetime: void f( const shared_ptr<widget>& w ); // (see next two slides…) • Pessimization (don’t do this): void f( shared_ptr<widget> w ); // atomic inc+dec

  23. Smart Pointer & Params, Note 1 • What if we modify a smart pointer passed by reference, and there’s an alias to the object that the shared_ptr is supposed to keep alive? shared_ptr<widget>& g_spw; f( g_spw, *g_spw ); void f( /*non-const*/ shared_ptr<widget>& spw, widget& w ) { :::spw = some_other_widget; // oopsw.draw(); // boom (if g_spw was the last) :::} • Solution: Pass by const&.

  24. Smart Pointer & Params, Note 2 • What if we modify the original object that was passed, and there’s an alias to the object that the shared_ptr is supposed to keep alive? shared_ptr<widget>& g_spw; f( g_spw, *g_spw ); void f( const shared_ptr<widget>& spw, widget& w ) {spw->draw(); // okg_spw = some_other_widget; // oopsw.draw(); // boom (if g_spw was the last)spw->draw(); // + no longer the same widget} • Solution: “Don’t do that.”

  25. Move Semantics and && the semantics of value types (no pointers! lifetime!) + the efficiency of reference types

  26. Move Semantics and && • Move is an optimization of copy (and enables perfect forwarding). • Use return by value to express “callee-allocated out.” • No change in guidance for “caller-allocated out.”

  27. References are Still References • Note: You wouldn’t do this: or this: widget&f() { widget* f() { widget w; widget w; ::: ::: return w; return &w;} } • So of course do this: not this (common initial mistake): widgetf() { widget&& f() { widget w; widget w;::: :::return w;return w;} } • NB: Returning a local or param treats it as an rvalue (it’s about to be destroyed).

  28. Lambdas pass code around like an object loop bodies predicates callbacks (and ~100 other examples)

  29. Lambdas • “Lambdas, Lambdas Everywhere” http://vimeo.com/23975522

  30. Algorithms++ • Did you just write a new algorithm? Example: for_each with step. template<class Coll, class Func>void for_each_nth( Coll& coll, step s, Func func ); • Congratulations! You’ve also written a new kind of loop. for_each_nth( v, 3, [] ( Widget& w ) { // for every 3rd element in v… :::}); • Great for parallel algorithms – without new language extensions. parallel_for_each( std::begin(v), std::end(v), [=](int i) {Foo( a[i] );} );

  31. Again: References are Still References • Note: You wouldn’t do this: or this: widget&f() { widget* f() { widget w; widget w; ::: ::: return w; return &w;} } • So of course do this: not this: or this: function<void()> f() { function<void()>f() { void f() { widget w; widget w; widget w;::: ::: async( [&w](){ g(w); } ); return [w](){ g(w); }; return [&w](){ g(w); }; :::} } }

  32. Clean, Safe, Fast • With C++11, modern C++ code is clean, safe, and fast. • As clean and safe as any other modern language. • When used in a modern style. • Was always fast. Now even faster. • Move semantics, constexpr.

  33. Roadmap • “C++11 feels like a new language.” • The most pervasive features. • Style, idioms, and guidance. • C++11 adoption scorecard. • How are we doing? • Learning, compilers, and books. • What’s next for ISO C++?

  34. We’re All Learning C++11 • The world is still in the early stages of gaining field experience using the new language features, individually and together. • Includes everyone: WG21 members, authors, developers. • Example: Move semantics. • 2002-09-10: N1377, Initial move proposal. (Hinnant, Dimov, Abrahams) • 2011-11-09: “We are still in an infancy stage with regard to having a good working knowledge of how to use rvalue refs. I would very much like us to strongly consider field experience in nailing down corner cases like this.” (Hinnant) • A few more: cbegin(x)/cend(x), uniform initialization guidance, range-for lookup.

  35. Libraries, Compilers, and Books: Oh My? • Using C++11: Implementations. • C++11 library: Several available now or soon (2012). • C++11 language: First reasonably fully conforming compiler in 2013? • Learning C++11: Books and other materials. • Scott Meyers’ “Overview of the New C++ (C++11)” – bit.ly/meyers11 • ETAs for the major books…

  36. Roadmap • “C++11 feels like a new language.” • The most pervasive features. • Style, idioms, and guidance. • C++11 adoption scorecard. • How are we doing? • Learning, compilers, and books. • What’s next for ISO C++?

  37. Suggestion: Preserve Style/Idiom/Guidance • Can still remove reasons to resort to preprocessor macros (they’re outside the language anyway). • Example: “#once” to replace #include guard macros. • Can still remove reasons to resort to template metaprogramming(arguably outside language, inaccessible to most developers anyway). • Example: “static if” to replace specialization/termination/SFINAE. • Can still make usability improvements to existing features (e.g., simpler spelling or wider use, not new style/idiom/guidance). • Polymorphic lambdas. []( some_typename& x ) { return x.f() + g(x); } []( auto&x ) { return x.f() + g(x); }  []( x ) { x.f() + g(x) }

  38. Q: What is Standard C++’s biggest weakness?

  39. Portable C++ proxies for size comparisons: spec #pages, book #pages language library C++11 C++98 + TR1 C++11 C++98 C++98 C11 C99 C11 C99 C90 C90 K&R K&R

  40. 2008 .NET FX + VS Pro Libs Java SE 7 2008 .NET FX (only) Portable C++ proxies for size comparisons: spec #words library #types (non-‘plumbing’) language library C++11 Java 7 (2011) C++11 C# 3.0 (2008)

  41. 2008 .NET FX + VS Pro Libs Java SE 7 2008 .NET FX (only) Portable C++ language library C++11 Java 7 (2011) C++11 C# 3.0 (2008)

  42. Portable C++ 2008 .NET FX + VS Pro Libs language library Java SE 7 2008 .NET FX (only) C++11 Java 7 (2011) C++11 C# 3.0 (2008)

  43. Portable C++ ? library “All in all, this [C++0x and post-C++0x library wish lists] … is not quite the ‘ambitious and opportunistic’ policy that I had hoped for in 2001 (§8). However, people who scream for more (such as me) should note that even what’s listed above will roughly double the size of the standard library.” – B. Stroustrup, HoPL-III, 2007 C++11 Java 7 (2011) C++11 C# 3.0 (2008)

  44. Portable C++ Library (PCL) • Goals: • Large set of useful and current libraries. • Available on all major platforms. • Shipped with and supported by C++ implementations. • And composable, using consistent types. • Minimum: De facto availability as part of all major compiler products. • Ideal: De jure inclusion in Standard C++.

  45. Wonderful! But… • … where are we going to find all these libraries? PCL WG21

  46. PCL Scope • Do focus on pure libraries. • Do focus on common modern tasks with state-of-the-art existing practice. • Lower-level: Message queue, ranges + range algorithms, parallel algorithms, thread-safe containers, continuations (future.then), async I/O, file system, networking/sockets, serialization. • Higher-level: REST web services, sensor fusion, HTTP, HTML, XML/XSLT, JSON, persistence, settings/preferences, compression, cryptography, audio/image/video, databases, SMS messaging. • Don’t target niche uses. (Example: Sci-eng linear algebra.) • Don’t become a platform = fat libs that duplicate native services. • Don’t attempt to define a “portable cross-platform” library that will be inferior to a native platform app. (Example: GUI WIMP widgets.)

  47. Roadmap • “C++11 feels like a new language.” • The most pervasive features. • Style, idioms, and guidance. • C++11 adoption scorecard. • How are we doing? • Learning, compilers, and books. • What’s next for ISO C++?

  48. Questions?

More Related