c++11 (formerly known as c++0x) is the new c++ language standard. dave abrahams, boostpro computing

114
C++ 2011 Highlights from the New International Standard 1 Monday, September 19, 2011

Upload: yaevents

Post on 10-May-2015

12.817 views

Category:

Technology


4 download

DESCRIPTION

Dave Abrahams, BoostPro ComputingHe is a founding member of Boost.org and an active participant in the ISO C++ standards committee. His broad range of experience in the computer industry includes shrink-wrap software development, embedded systems design and natural language processing. He has authored eight Boost libraries and has made contributions to numerous others. Dave made his mark on C++ standardization by developing a conceptual framework for understanding exception-safety and applying it to the C++ standard library. He created the first exception-safe standard library implementation and, with Greg Colvin, drafted the proposals that eventually became the standard library’s exception safety guarantees.Presentation topic:C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing.Key points:The ISO C++ standardization committee has just unanimously approved its final draft international standard, and it's chock full of new features. Though a few of the features have been available for years, some are brand new, and nobody really knows what it's like to program in this new C++ language. As with C++03, Boost.org is expected to take a leading role in exploiting C++11. In this talk, I'll give an overview of the most important new developments.

TRANSCRIPT

Page 1: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

C++ 2011Highlights from the New International Standard

1

Monday, September 19, 2011

Page 2: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Intro

• Who am I

• Who are you?

• Why We’re Here

2

Monday, September 19, 2011

Page 3: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

C++11 Goals

• Make C++ a better language for systems

programming

• Make C++ a better language for writing libraries

• Make C++ more teachable and learnable

• Maintain backward compatibility

3

Monday, September 19, 2011

Page 4: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

C++11 At a Glance

• General Core Language Features

concurrency, move semantics, auto, range-based for, lambdas…

• Library Features

containers, regular expressions, smart pointers, new algorithms…

• Features for writing classes

constructor delegation, override/final, =default/=delete…

• Crazy Template Stuff

Variadic templates, template aliases, decltype, perfect forwarding, …

4

Monday, September 19, 2011

Page 5: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Ponies for Everybody!Big wins from little features

5

Monday, September 19, 2011

Page 6: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

6

void print_3x(vector<float> const& v){ for ( vector<float>::const_iterator p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 7: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

6

void print_3x(vector<float> const& v){ for ( vector<float>::const_iterator p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 8: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

7

void print_3x(vector<float> const& v){ for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 9: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

8

void print_3x(vector<float> const& v){ for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 10: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

9

auto a = 3; intconst auto b = 3.14; const doubleauto const& c = 42; int const&

auto d = "foobar"; char const*auto& e = "baz" char const(&)[4]

extern std::list<Foo> l;auto p = l.begin(); std::list<Foo>::iteratorauto& x = l.front(); std::list<Foo>::value_type&

auto& y = any-expression-here;

Monday, September 19, 2011

Page 11: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Type Deduction

10

void print_3x(vector<float> const& v){ for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 12: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

11

void print_3x(vector<float> const& v){ for ( auto p = v.begin(); p != v.end(); p++ ) { std::cout << *p * 3 << " "; }}

Monday, September 19, 2011

Page 13: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

12

void print_3x(vector<float> const& v){ for ( float a: v ) { std::cout << a * 3 << " "; }}

Monday, September 19, 2011

Page 14: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

12

void print_3x(vector<float> const& v){ for ( float a: v ) { std::cout << a * 3 << " "; }}

Range

Monday, September 19, 2011

Page 15: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

12

void print_3x(vector<float> const& v){ for ( float a: v ) { std::cout << a * 3 << " "; }}

Loop variable Range

Monday, September 19, 2011

Page 16: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

auto in C++11Range-based for loop

13

void print_3x(vector<float> const& v){ for ( auto a: v ) { std::cout << a * 3 << " "; }}

Monday, September 19, 2011

Page 17: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

14

template <class Range>void print_3x(Range const& v){ for ( typename Range::value_type a: v ) { std::cout << a * 3 << " "; }}

Monday, September 19, 2011

Page 18: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

auto in C++11Range-based for loop

15

template <class Range>void print_3x(Range const& v){ for ( auto a: v ) { std::cout << a * 3 << " "; }}

Monday, September 19, 2011

Page 19: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

auto in C++11Range-based for loop

16

void inplace_3x(vector<float>& v){ for ( float& a: v ) { a *= 3; }}

Monday, September 19, 2011

Page 20: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Ranges Everywhere

17

int rng[3] = { 42, 314, 77 };for( int var: rng ){ ...}

Monday, September 19, 2011

Page 21: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Ranges Everywhere

18

std::pair<Iter,Iter> rng = … ;for( int var: rng ){ ...}

Monday, September 19, 2011

Page 22: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Ranges Everywhere

19

YourType rng;for( int var: rng ){ ...}

Monday, September 19, 2011

Page 23: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

20

void print_3x(vector<float> const& v){ BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; }}

C++98

Monday, September 19, 2011

Page 24: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

20

void print_3x(vector<float> const& v){ BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; }}

“ ”C++98

Monday, September 19, 2011

Page 25: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Range-based for loop

20

void print_3x(vector<float> const& v){ BOOST_FOREACH( float a, v ) { std::cout << a * 3 << " "; }}

“ ”C++98

See your friendly neighborhood Boost C++ Libraries for details

Monday, September 19, 2011

Page 26: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

Monday, September 19, 2011

Page 27: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

Monday, September 19, 2011

Page 28: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

Monday, September 19, 2011

Page 29: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

static void print_3x_element(float a){ std::cout << a * 3 << " ";}

Monday, September 19, 2011

Page 30: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

static void print_3x_element(float a){ std::cout << a * 3 << " ";}

print_3x_element

Monday, September 19, 2011

Page 31: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Using an Algorithm

21

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(), );}

static void print_3x_element(float a){ std::cout << a * 3 << " ";}

print_3x_element

Monday, September 19, 2011

Page 32: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Lambda Expressions

22

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

);}

Monday, September 19, 2011

Page 33: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Lambda Expressions

22

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

);}

“Lambda expression”

[](float a) { std::cout << a * 3 << " "; }

Monday, September 19, 2011

Page 34: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Lambda Expressions

23

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

Monday, September 19, 2011

Page 35: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Lambda Expressions

23

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

introducer

Monday, September 19, 2011

Page 36: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

What It Is

24

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

Monday, September 19, 2011

Page 37: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

What It Is

24

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

struct __lambda143{ inline void operator()(float a) const { std::cout << a * 3 << " "; }};

anonymous class

Monday, September 19, 2011

Page 38: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

What It Is

24

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

struct __lambda143{ inline void operator()(float a) const { std::cout << a * 3 << " "; }};

anonymous class

Monday, September 19, 2011

Page 39: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

What It Is

24

void print_3x(vector<float> const& v){ std::for_each( v.begin(), v.end(),

[](float a) { std::cout << a * 3 << " "; } );}

struct __lambda143{ inline void operator()(float a) const { std::cout << a * 3 << " "; }};

__lambda143()

anonymous class

“closure” instance

Monday, September 19, 2011

Page 40: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

std::vector<std::pair<int, std::string>> x;

Closing Angle Brackets

25

Monday, September 19, 2011

Page 41: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

std::vector<std::pair<int, std::string>> x;

Closing Angle Brackets

25

Error in C++98: >> tokenized as right-shift

Dummy

Monday, September 19, 2011

Page 42: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

std::vector<std::pair<int, std::string>> x;

Closing Angle Brackets

25

OK in C++11

Dummy

Monday, September 19, 2011

Page 43: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move SemanticsA Really Cheap Lunch

26

Monday, September 19, 2011

Page 44: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

Monday, September 19, 2011

Page 45: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

Monday, September 19, 2011

Page 46: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

Monday, September 19, 2011

Page 47: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A

Monday, September 19, 2011

Page 48: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B

Monday, September 19, 2011

Page 49: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C

Monday, September 19, 2011

Page 50: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

Monday, September 19, 2011

Page 51: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

Monday, September 19, 2011

Page 52: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

A

Monday, September 19, 2011

Page 53: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

A B

Monday, September 19, 2011

Page 54: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

A B C

Monday, September 19, 2011

Page 55: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

27

• A B C D

A B C D

Monday, September 19, 2011

Page 56: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

• A B C D

A B C D

Monday, September 19, 2011

Page 57: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

A B C D

Monday, September 19, 2011

Page 58: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

• A B C D

Monday, September 19, 2011

Page 59: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

• A B C D E

Monday, September 19, 2011

Page 60: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

• A B C D E

Monday, September 19, 2011

Page 61: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Copy Fatigue?

28

• A B C D E

Expensive copies are made just

before destroying the source

object

Monday, September 19, 2011

Page 62: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

• B C DA

Monday, September 19, 2011

Page 63: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

• B C D

A

Monday, September 19, 2011

Page 64: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

B

C D

A

Monday, September 19, 2011

Page 65: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

B C

D

A

Monday, September 19, 2011

Page 66: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

B C DA

Monday, September 19, 2011

Page 67: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

29

B C DA

Monday, September 19, 2011

Page 68: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

30

A B C D

Monday, September 19, 2011

Page 69: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

30

• A B C D

Monday, September 19, 2011

Page 70: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

30

• A B C D E

Monday, September 19, 2011

Page 71: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

typedef set<string> record;typedef vector<record> database;

database load_db(string filename);database db = load_db("huge.db");

db.push_back( create_record() );

Try Moving Instead!

30

• A B C D E

Monday, September 19, 2011

Page 72: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Mini-HOWTOMove-enabling an existing class

31

Monday, September 19, 2011

Page 73: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 74: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 75: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 76: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 77: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 78: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

X

Move-Enabling

32

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• X

Monday, September 19, 2011

Page 79: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

• Z

Monday, September 19, 2011

Page 80: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

• Z

Monday, September 19, 2011

Page 81: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

• Z

Monday, September 19, 2011

Page 82: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

Monday, September 19, 2011

Page 83: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

Monday, September 19, 2011

Page 84: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

Monday, September 19, 2011

Page 85: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

Monday, September 19, 2011

Page 86: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Y

Move-Enabling

33

struct Owner { Owner(const Owner& src) : p(src.p ? new Resource(*src.p) : 0) {}

Owner& operator=(const Owner& src) { if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

~Owner() { delete p; }private: Resource* p;};

• Y

Monday, September 19, 2011

Page 87: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

New Move Operations

{ if (this == &src) return *this; delete p; p = 0; if (src.p) p = new Resource(*src.p); return *this; }

34

struct Owner { Owner(const Owner& src)

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(const Owner& src)

: p(src.p ? new Resource(*src.p) : 0) {}

Monday, September 19, 2011

Page 88: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

New Move Operations

34

struct Owner { Owner(const Owner& src)

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(const Owner& src)

Monday, September 19, 2011

Page 89: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

New Move Operations

34

struct Owner { Owner(const Owner& src)

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(const Owner& src)

Monday, September 19, 2011

Page 90: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

X• X

Monday, September 19, 2011

Page 91: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

X• X

Monday, September 19, 2011

Page 92: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

Monday, September 19, 2011

Page 93: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

rvalue reference

Monday, September 19, 2011

Page 94: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

rvalue reference

Monday, September 19, 2011

Page 95: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

Monday, September 19, 2011

Page 96: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

Monday, September 19, 2011

Page 97: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

Monday, September 19, 2011

Page 98: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Constructor

35

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

~Owner() { delete p; }private: Resource* p;};

Owner(Owner&& src) : p(src.p) { src.p = 0; }

X• X

Monday, September 19, 2011

Page 99: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Y•

• Z

Monday, September 19, 2011

Page 100: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y•

• Z

Monday, September 19, 2011

Page 101: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y•

• Z

Monday, September 19, 2011

Page 102: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y•

• Z

Monday, September 19, 2011

Page 103: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y•

Monday, September 19, 2011

Page 104: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y•

Monday, September 19, 2011

Page 105: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Assignment

36

struct Owner { Owner(const Owner& src); Owner& operator=(const Owner& src);

Owner(Owner&& src) : p(src.p) { src.p = 0; }

~Owner() { delete p; }private: Resource* p;};

Owner& operator=(Owner&& src) { if (this == &src) return *this; delete p; p = src.p; src.p = 0; return *this; }

Y

Monday, September 19, 2011

Page 106: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Move Semantics

• Automatically accelerates existing code!

• Applied throughout standard library

• Many move-only types in library

• Launches a new era of value semantics

37

Monday, September 19, 2011

Page 107: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Containers

• Move semantics

• Hash containers

• forward_list

• array

• tuple

• Emplace

• Allocators

38

Monday, September 19, 2011

Page 108: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Other Useful Libraries

• regex:

• Algorithms

• function / bind

39

Monday, September 19, 2011

Page 109: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Smart Pointers

• unique_ptr

• shared_ptr

40

Monday, September 19, 2011

Page 110: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Classes and OOP

• member initializers

• explict conversion

• override and final

• delegating and inheriting constructors

• defaulted and deleted functions

41

Monday, September 19, 2011

Page 111: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Concurrency

• async, futures, promise

• locks, mutexes and condition variables

• exception propagation

• atomics

42

Monday, September 19, 2011

Page 112: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Templates

• Variadics

• static_assert

• local classes as template arguments

• extern templates

• Template aliases

43

Monday, September 19, 2011

Page 113: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

Metaprogramming

• constexpr

• Template aliases

• User-defined literals w/variadic templates

• Type traits and extended SFINAE

44

Monday, September 19, 2011

Page 114: C++11 (formerly known as C++0x) is the new C++ language standard. Dave Abrahams, BoostPro Computing

What’s Next?

• Modules

• Concepts

• Pythy Syntax:

• []min(x, y) { return x < y ? x : y }

45

Monday, September 19, 2011