herb sutter. c/c++ 7.0 feb 1992 vc++11 beta: feb 2012 arm targeting win8 tablet apps c++ amp...

Post on 23-Dec-2015

217 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Herb Sutter

20 Year Anniversary

C/C++ 7.0Feb 1992

VC++11Beta: Feb 2012

ARM targetingWin8 tablet apps

C++ AMPComplete C++11 stdlib

C++ AMPPr

oces

sors

MemoryUnified Disjoint

(tight)NUMA cache NUMA RAM Incoherent /

weak

Complex OoO

Simple InO

Specialized(X)

Specialized(Y)

Disjoint (loose)

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

June 2011

C++ AMP Open Specification

Todayblogs.msdn.com/nativeconcurrency/

VC++ & C++11

VC11Beta

VC11RTM

OOBCTP

OOBRTM

Feb2012

+ C++11range-for,

final,override

Out-Of-BandReleases

+ progressively roll out C++11 initializer lists, template

aliases, variadic templates, constexpr, noexcept,

=default/delete, …

VC11DP

Sep2011

+ completeC++11 stdlib:

thread/mutex,async, future, …+ track changes

(lambdas, &&, …)

You arehere

VC10RTM

Apr2010

+ lambdas, move/&&,

auto, decltype,auto fn decls,

extern template,nullptr, … Survey: bit.ly/mscpp11

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++?

“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:

Core Languageauto nullptrrange-for lambdasmove semantics, rvalue references

uniform initialization, initializer lists

noexcept constexpr

Librarysmart pointersbegin(x) / end(x)

auto

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

C++98 C++11

map<int,string>::iterator i = m.begin();double const xlimit = config["xlimit"];singleton& s = singleton::instance();

auto i = begin(m);auto const xlimit = config["xlimit"];auto& s = singleton::instance();

binder2nd<greater> x = bind2nd( greater(), 42 ); auto x = [](int i) { return i > 42; };

Nothing new here

Exactly the same as

template arg deduction

Uniform Initialization and initializer_lists

{ }

Uniform Initialization and initializer_listsC++98

rectangle w( origin(), extents() );complex<double> c ( 2.71828, 3.14159 );int a[] = { 1, 2, 3, 4 };vector<int> v;for( int i = 1; i <= 4; ++i ) v.push_back(i);

Uniform Initialization and initializer_listsC++98 C++11

rectangle w( origin(), extents() );complex<double> c ( 2.71828, 3.14159 );int a[] = { 1, 2, 3, 4 };vector<int> v;for( int i = 1; i <= 4; ++i ) v.push_back(i);

rectangle w { origin(), extents() };complex<double> c { 2.71828, 3.14159 };int a[] { 1, 2, 3, 4 };vector<int> v { 1, 2, 3, 4 };

Uniform Initialization and initializer_listsC++98 C++11

rectangle w( origin(), extents() );complex<double> c ( 2.71828, 3.14159 );int a[] = { 1, 2, 3, 4 };vector<int> v;for( int i = 1; i <= 4; ++i ) v.push_back(i);

rectangle w { origin(), extents() };complex<double> c { 2.71828, 3.14159 };int a[] { 1, 2, 3, 4 };vector<int> v { 1, 2, 3, 4 };

int i = 42; int i = 42;auto x = begin(v);

Uniform Initialization and initializer_listsC++98 C++11

rectangle w( origin(), extents() );complex<double> c ( 2.71828, 3.14159 );int a[] = { 1, 2, 3, 4 };vector<int> v;for( int i = 1; i <= 4; ++i ) v.push_back(i);

rectangle w { origin(), extents() };complex<double> c { 2.71828, 3.14159 };int a[] { 1, 2, 3, 4 };vector<int> v { 1, 2, 3, 4 };

int i = 42; int i = 42;auto x = begin(v);

draw_rect( rectangle( obj.origin,selection.extents ) );

draw_rect( { obj.origin, selection.extents } );

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>

for, begin, end

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.)

C++98 C++11

for( vector<int>::iterator i = v.begin(); i != v.end(); ++i ) { total += *i;}

for( auto d : v ) { total += d;}

sort( v.begin(), v.end() );sort( &a[0], &a[0] + sizeof(a)/sizeof(a[0]) );

sort( begin(v), end(v) );sort( begin(a), end(a) );

cbegin, cend An oversight:

find_if( begin(v), end(v), ::: ); // C++11find_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(); }

Smart Pointers

standard and portable memory safety

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.

C++98 C++11widget* pw = new widget();delete pw;

auto pw = make_shared<widget>();

class node { vector<node*> children; node* parent;

class node { vector<unique_ptr<node>> children; node* parent;

DELETE

make_unique An oversight:

auto p1 = make_shared<widget>(); // C++11auto 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)... ) );}

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

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; // oops w.draw(); // boom (if g_spw was the last) :::}

Solution: Pass by const&.

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(); // ok g_spw = some_other_widget; // oops w.draw(); // boom (if g_spw was the last) spw->draw(); // + no longer the same widget}

Solution: “Don’t do that.”

Move Semantics and &&

the semantics of value types (no pointers! lifetime!)

+the efficiency of reference types

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.”

C++98 C++11// no copy, but don’t forget to deletevector<int>* make_big_vector();

// no copy, but commit to shared_ptr/refcntshared_ptr<vector<int>> make_big_vector();

// Use this for “callee-allocated out”vector<int> make_big_vector();

// callee-allocated outvoid make_big_vector( vector<int>& out );

// callee-allocated outvoid make_big_vector( vector<int>& out );

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):widget f() { 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).

Lambdas

pass code around like an object

loop bodiespredicatescallbacks

(and ~100 other examples)

Lambdas

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

C++98 C++11

vector<int>::iterator i = v.begin();for( ; i != v.end(); ++i ) { if( *i > x && *i < y ) break;}

auto i = find_if( begin(v), end(v), [=](int i) { return i > x && i < y; } );

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] );} );

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); }; :::} } }

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.

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++?

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.

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…

C++ Primer (Moo) Aug 2012 ReferenceThe C++ Programming Language (Stroustrup) Late 2012 ReferenceProgramming: Principles & Practice Using C++ (Str.) Late 2013? IntroductoryEffective C++ (Meyers) 2013-14? Style guidanceC++ Coding Standards (Sutter, Alexandrescu) 2015? Established guidance

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++?

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) }

Q: What is Standard C++’sbiggest weakness?

C++11

C++98 + TR1C++11

Portable C++

C++98 C++98

language

C11C11

library

C99C99

C90 C90K&RK&R

proxies for size comparisons: spec #pages, book #pages

proxies for size comparisons: spec #words library #types (non-‘plumbing’)

2008 .NET FX + VS Pro Libs

Java SE 7

C++11

Portable C++language

C# 3.0 (2008)Java 7 (2011)

2008 .NET FX (only)

C++11

library

2008 .NET FX + VS Pro Libs

Java SE 7

C++11

C# 3.0 (2008)Java 7 (2011)

2008 .NET FX (only)

C++11

language library

Portable C++

2008 .NET FX + VS Pro Libs

Java SE 7

C++11

C# 3.0 (2008)Java 7 (2011)

2008 .NET FX (only)

Portable C++

C++11

language library

C++11

C# 3.0 (2008)Java 7 (2011)

Portable C++

?C++11

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

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++.

Wonderful! But… … where are we going to find all these libraries?

WG21

PCL

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.)

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++?

Questions?

top related