Presentation is loading. Please wait.

Presentation is loading. Please wait.

Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary.

Similar presentations


Presentation on theme: "Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary."— Presentation transcript:

1

2 Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary

3 Motivation and Goal  There’s a growing wave of developers who are turning or returning to C++.  You know how to program.  You may “know” things about C++ that have changed.  You may not be familiar with how elegant it can be to write code in today’s C++.  This talk shows how clean, safe, and fast C++ code is today. power: driver at all scales – on-die, mobile, desktop, datacenter size: limits on processor resources – desktop, mobile experiences: bigger experiences on smaller hardware; pushing envelope means every cycle matters

4 Core Value Mindset  Achieve C++’s value proposition of efficient abstraction.  Strong abstraction: Type-safe OO and generic code for modeling power, without sacrificing control and efficiency.  Full control over code and memory: You can always express what you want to do. And you can always control memory and data layout exactly.  Pay-as-you-go efficiency: No mandatory overheads, don’t pay for what you don’t use. “The going word at Facebook is that ‘ reasonably written C++ code just runs fast,’ which underscores the enormous effort spent at optimizing PHP and Java code. Paradoxically, C++ code is more difficult to write than in other languages, but efficient code is a lot easier.” – Andrei Alexandrescu

5 Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary

6 What’s Different: At a Glance Then Now circle* p = new circle( 42 ); vector vw = load_shapes(); for( vector ::iterator i = vw.begin(); i != vw.end(); ++i ) { if( *i && **i == *p ) cout << **i << “ is a match\n”; } for( vector ::iterator i = vw.begin(); i != vw.end(); ++i ) { delete *i; } delete p; auto p = make_shared ( 42 ); vector > vw = load_shapes(); for_each( begin(vw), end(vw), [&]( shared_ptr & s ) { if( s && *s == *p ) cout << *s << “ is a match\n”; } ); T*  shared_ptr new  make_shared no need for “delete” automatic lifetime management exception-safe for/while/do  std:: algorithms [&] lambda functions auto type deduction not exception-safe missing try/catch, __try/__finally

7

8 Some Great Things Never Change: Automatic Lifetime = Efficient + Exception Safe class widget { private: gadget g; public: void draw(); }; void f() { widget w; ::: w.draw(); ::: } lifetime automatically tied to enclosing object no leak, exception safe lifetime automatically tied to enclosing scope constructs w, including the w.g gadget member Automatic destruction and deallocation for w and w.g Automatic exception safety, as if “finally { w.dispose(); w.g.dispose(); }”

9 Heap Lifetime: Standard Smart Pointers class gadget; class widget { private: shared_ptr g; }; class gadget { private: weak_ptr w; }; class node { vector > children; node* parent; ::: public: node( node* parent_) : parent(parent_) { children.push_back( new node(…) ); ::: } }; shared ownership still keeps gadget alive w/auto lifetime mgmt no leak, exception safe use weak_ptr to break reference-count cycles unique ownership node owns its children no leak, exception safe node observes its parent plain “new” should immediately initialize another object that owns it, usually a unique_ptr If/when performance optimization is needed, consider well-encapsulated uses of owning *’s and delete (e.g., hidden inside objects). Example: writing your own low-level data structure.

10 “C++ is the best language for garbage collection principally because it creates less garbage.” — Bjarne Stroustrup

11 Containers vector v; v.push_back( “Geddy Lee” ); array a; map phone; phone[“Alex Lifeson”] = “+1 (416) 555-1212”; multimap phone; phone[“Neil Peart”] = “+1 (416) 555-1212”; phone[“Neil Peart”] = “+1 (905) 555-1234”; unordered_map phone; phone[“Alex Lifeson”] = “+1 (416) 555-1212”; unordered_multimap phone; phone[“Neil Peart”] = “+1 (416) 555-1212”; phone[“Neil Peart”] = “+1 (905) 555-1234”; default container: vector compact, efficient: cache-friendly, prefetcher-friendly dictionary: map (tree) or unordered_map (hash) fixed size vector: array compact, efficient: cache-friendly, prefetcher-friendly

12 Loops Then Now for( auto i = v.begin(); i != v.end(); ++i ) { ::: ::: ::: } auto i = v.begin(); for( ; i != v.end(); ++i ) { if (*i > x && *i < y) break; } for_each( begin(v), end(v), []( string& s ) { ::: ::: ::: } ); auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } ); for/while/do  std:: algorithms [&] lambda functions for_each to visit each element find_if to find a match prefer non-member begin()/end() arbitrary length lambda bodies, just put the loop body inside the lambda

13 Algorithms  By default, reach for:  for_each: The default traversal algorithm.  Also transform for not-in-place semantics.  find_if: The default search algorithm.  sort, lower_bound, et al.: The default sorting and searching.  To write a comparator: Use strict <, prefer named lambdas. auto comp =[]( const widget& w1, const widget& w2 ) { return w1.weight() < w2.weight(); } sort( v.begin(), v.end(), comp ); auto i = lower_bound( v.begin(), v.end(), comp );

14 Value Types & Reference Types “Copyable” Value Types (Default) “Polymorphic” Reference Types class point { int x; int y; public: ::: }; class shape : boost::noncopyable { public: virtual draw(); ::: }; class circle : shape { public: virtual draw() override; ::: }; all about base classes and virtual functions all about memory and layout control explicitly disable copying disabling is inherited explicit override control

15 Value Types and Move Efficiency set load_huge_data() { set ret; // … load data and populate ret … return ret; } widgets = load_huge_data(); vector v = IfIHadAMillionStrings(); v.insert( begin(v)+v.size()/2, “tom” ); v.insert( begin(v)+v.size()/2, “richard” ); v.insert( begin(v)+v.size()/2, “harry” ); HugeMatrix operator+( const HugeMatrix&, const HugeMatrix& ); hm3 = hm1+hm2; efficient, no deep copy no need for “heap allocation + return a pointer” workaround efficient, no deep copy-shuffle (just 1.5M ptr/len assignments) efficient, no extra copies

16 Enable Move For Appropriate Value Types class my_class { unique_ptr data; public: my_class( my_class&& other ) : data( move( other.data ) ) { } my_class& operator=( my_class&& other ) { data = move( other.data ); } ::: void method() { if( !data ) throw “moved-from object”; ::: } }; move construction move assignment check (if appropriate) Copy  ? Move: Also enable move if it can be cheaper than a deep copy. Move  / Copy: Some non-value types are naturally move-only. Example: unique_ptr.

17 Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary

18 Pimpl For Compile Time Encapsulation (C++  C++)  Use the Pimpl idiom judiciously to truly hide private members. class my_class { // … all public and protected stuff goes here … private: class impl; unique_ptr pimpl;// opaque type here }; class my_class::impl {// defined privately here // … all private data and functions: all of these // can now change without recompiling callers … }; my_class::my_class() : pimpl( new impl ) { /* … set impl values …*/ }  Avoids rebuild cascades and brittle object layouts.  Most appropriate for (transitively) popular types. my_class.h my_class.cpp

19 Use Sufficiently Portable Types and Conventions At ABI Boundaries (C++  * )  Portable types = C built-in types (incl. *) and structs thereof.  Class types can only be used when caller and callee agree on layout, calling convention, etc. = compiled with same compiler + settings.  When callers may be compiled with another compiler/language:  “Flatten” to an “extern C” API with a specific calling convention. class widget { widget(); ~widget(); double method( int, gadget& ); }; extern “C” {// functions using explicit “this” struct widget;// + opaque type (fwd decl only) widget* STDCALL widget_create();// ctor  create “this” void STDCALL widget_destroy( widget* );// dtor  nuke “this” double STDCALL widget_method( widget*, int, gadget* );// use “this” }

20 WinRT Types: For Cross-Language Use (C++  JS/.NET ) Module Internals written in C++ Module Internals written in C++ WinRT External Surface for WinRT callers/callees WinRT External Surface for WinRT callers/callees C/C++ External Surface use: “C” handle style, C++ Pimpl style C/C++ External Surface use: “C” handle style, C++ Pimpl style

21 C++ & WinRT Standard C++: Portable & Efficient Types C++/CX: WinRT & ABI-Safe Types class point { int x; int y; }; class drawable : boost::noncopyable { public: virtual void draw() = 0; }; class shape : public drawable { … }; class circle : public shape { … }; ::: auto p = make_shared (); shared_ptr p2 = p; p2->draw(); value class Point { int x; int y; }; interface class IDrawable { virtual void Draw(); }; ref class Shape : IDrawable { … }; ref class Circle : Shape { … }; ::: auto p = ref new Circle(); IDrawable^ p2 = p; p2->draw(); types are ABI-safe & WinRT cross-language can state the kind of type you’re writing: value type, reference type, or interface two main concepts: ^ and ref new

22 Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary

23 C++ Renaissance  Industry momentum for C++.  Renewing our commitment to C++:  New programming model for Windows 8.  Get the full power of your CPU and GPU.  Use Windows 8 hardware capabilities to the fullest.  Build for ARM. Visual C++: The power and performance tool for Windows.

24 TOOL-100T: Improving software quality using Visual Studio 11 C++ Code Analysis TOOL-479T: A lap around Visual Studio 11 Express for Metro style apps using C++ TOOL-532T: Using the Windows Runtime from C++ TOOL-761T: A lap around DirectX game development tools TOOL-802T: Taming GPU compute with C++ AMP TOOL-845T: Tips and tricks for developing Metro style apps using C++ RELATED SESSIONS Bringing Existing C++ Code into Metro Style Apps Ale Contenti Under the Covers with C++ for Metro style Apps Deon Brewis Building Metro style apps with HTML5 and Compiled Code Raman Sharma Why C++: C++ Renaissance Herb Sutter ONLINE TALKS ON CHANNEL 9

25 Q&A

26

27

28


Download ppt "Roadmap  Motivation and Scope  Modern C++: Clean, Safe, Fast  Across Modules: ABI Safety & Other Languages  Summary."

Similar presentations


Ads by Google