20130110 prs presentation ncim c++ 11

134
C++ 11 Ralph Langendam NCIM-Groep Januari 9th, 2013 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 1 / 39

description

NCIM Techtalk presentation about C++11 by Ralph Langendam

Transcript of 20130110 prs presentation ncim c++ 11

Page 1: 20130110 prs presentation ncim c++ 11

C++ 11

Ralph Langendam

NCIM-Groep

Januari 9th, 2013

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 1 / 39

Page 2: 20130110 prs presentation ncim c++ 11

Overview

1 TimelineHistoryPresent and Future

2 C++11 DevelopmentDirectivesEnhancement categories

3 Run-time performanceenhancements

Move semanticsConstant expression

4 Usability enhancementsStatic assertionsType inference

Anonymous functionsOverride and FinalAlias templatesEnumerations and Unions

5 New functionalityVariadic templatesDefaulted and deleted specialmethods

6 Standard library enhancementsThreading facilitiesSmart pointersType traits

7 Compiler support8 Further reading

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 2 / 39

Page 3: 20130110 prs presentation ncim c++ 11

Timeline

Outline

1 TimelineHistoryPresent and Future

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancements

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further readingRalph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 3 / 39

Page 4: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 5: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 6: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 7: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 8: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 9: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 10: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 11: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 12: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 13: 20130110 prs presentation ncim c++ 11

Timeline History

History

≈ 1979 C with classes (CFront): C, classes, inheritance, inline,default argument values, strong type checking.

1983 C++ : exceptions, virtual functions, function overloading,const, references, comment (//).

1985 Publication of Bjarne Stroustrup - The C++ ProgrammingLanguage.

1989 protected and static members.

1990 Publication of The Annotated C++ Reference Manual,Borland Turbo C++.

1998 Standardization by ISO (C++98) and development of STL.

2003 C++03

2005 TR1 with features for C++0x.

2011 C++11

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39

Page 14: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 15: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 16: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 17: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 18: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 19: 20130110 prs presentation ncim c++ 11

Timeline Present and Future

Present and Future

The Boost library project was a large source of inspiration to TR1.

Nowadays Boost is the main incubator for new C++ features.

Bugfixes for C++14.

TR2 for C++17.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39

Page 20: 20130110 prs presentation ncim c++ 11

C++11 Development

Outline

1 Timeline

2 C++11 DevelopmentDirectivesEnhancement categories

3 Run-time performance enhancements

4 Usability enhancements

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further readingRalph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 6 / 39

Page 21: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 22: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 23: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 24: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 25: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 26: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 27: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 28: 20130110 prs presentation ncim c++ 11

C++11 Development Directives

Directives

Backwards compatibility with C++98 and C.

Extend primarily through STL and less through C++ core language.

Focus on programming techniques, systems and library design.

Increase type safety.

Increase performance and ability to work directly with hardware.

Implement the zero overhead principle.

Make C++ easier to learn.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39

Page 29: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 30: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 31: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 32: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 33: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 34: 20130110 prs presentation ncim c++ 11

C++11 Development Enhancement categories

Enhancement categories

Run-time performance enhancements

Build-time performance enhancements

Usability enhancements

New functionality

Standard library enhancements

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39

Page 35: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancementsMove semanticsConstant expression

4 Usability enhancements

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further readingRalph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 9 / 39

Page 36: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

C++ distinguishes between l-values and r-values.

int l;

l = 3 // Assign r-value 3 to l-value l

4 = l // Error: 4 is not an l-value

Now, consider

int PlusOne (int n) {

return n+1;

}

int result (PlusOne (3+4));

1 3+4 is evaluated and copied to a new int n (7) in PlusOne.2 n+1 is evaluated and copied to a new int result (8).

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39

Page 37: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

C++ distinguishes between l-values and r-values.

int l;

l = 3 // Assign r-value 3 to l-value l

4 = l // Error: 4 is not an l-value

Now, consider

int PlusOne (int n) {

return n+1;

}

int result (PlusOne (3+4));

1 3+4 is evaluated and copied to a new int n (7) in PlusOne.2 n+1 is evaluated and copied to a new int result (8).

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39

Page 38: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

C++ distinguishes between l-values and r-values.

int l;

l = 3 // Assign r-value 3 to l-value l

4 = l // Error: 4 is not an l-value

Now, consider

int PlusOne (int n) {

return n+1;

}

int result (PlusOne (3+4));

1 3+4 is evaluated and copied to a new int n (7) in PlusOne.2 n+1 is evaluated and copied to a new int result (8).

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39

Page 39: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

C++ distinguishes between l-values and r-values.

int l;

l = 3 // Assign r-value 3 to l-value l

4 = l // Error: 4 is not an l-value

Now, consider

int PlusOne (int n) {

return n+1;

}

int result (PlusOne (3+4));

1 3+4 is evaluated and copied to a new int n (7) in PlusOne.

2 n+1 is evaluated and copied to a new int result (8).

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39

Page 40: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

C++ distinguishes between l-values and r-values.

int l;

l = 3 // Assign r-value 3 to l-value l

4 = l // Error: 4 is not an l-value

Now, consider

int PlusOne (int n) {

return n+1;

}

int result (PlusOne (3+4));

1 3+4 is evaluated and copied to a new int n (7) in PlusOne.2 n+1 is evaluated and copied to a new int result (8).

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39

Page 41: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

Using r-value references we can get rid of the first copy:

int PlusOne (int&& n) {

return n+1;

}

The evaluated integer instance 7 is used in evaluating n+1.

STL containers benefit from this too:

std:: vector <int > v;

// Use void std:: vector <T, A>:: push_back (T&&)

v.push_back (3+4);

int r (7);

// Use void std:: vector <T, A>:: push_back (T const &)

v.push_back (r);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39

Page 42: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

Using r-value references we can get rid of the first copy:

int PlusOne (int&& n) {

return n+1;

}

The evaluated integer instance 7 is used in evaluating n+1.

STL containers benefit from this too:

std:: vector <int > v;

// Use void std:: vector <T, A>:: push_back (T&&)

v.push_back (3+4);

int r (7);

// Use void std:: vector <T, A>:: push_back (T const &)

v.push_back (r);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39

Page 43: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

Using r-value references we can get rid of the first copy:

int PlusOne (int&& n) {

return n+1;

}

The evaluated integer instance 7 is used in evaluating n+1.

STL containers benefit from this too:

std:: vector <int > v;

// Use void std:: vector <T, A>:: push_back (T&&)

v.push_back (3+4);

int r (7);

// Use void std:: vector <T, A>:: push_back (T const &)

v.push_back (r);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39

Page 44: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Move semantics

R-value references

Using r-value references we can get rid of the first copy:

int PlusOne (int&& n) {

return n+1;

}

The evaluated integer instance 7 is used in evaluating n+1.

STL containers benefit from this too:

std:: vector <int > v;

// Use void std:: vector <T, A>:: push_back (T&&)

v.push_back (3+4);

int r (7);

// Use void std:: vector <T, A>:: push_back (T const &)

v.push_back (r);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39

Page 45: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant expression

Compile time calculations are ill-formed.

int Two () {

return 2;

}

double array [Two () + 3]; // Ill -formed

The compiler is not aware of Two being constant.

We resolve this with constexpr.

constexpr int Two () {

return 2;

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39

Page 46: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant expression

Compile time calculations are ill-formed.

int Two () {

return 2;

}

double array [Two () + 3]; // Ill -formed

The compiler is not aware of Two being constant.

We resolve this with constexpr.

constexpr int Two () {

return 2;

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39

Page 47: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant expression

Compile time calculations are ill-formed.

int Two () {

return 2;

}

double array [Two () + 3]; // Ill -formed

The compiler is not aware of Two being constant.

We resolve this with constexpr.

constexpr int Two () {

return 2;

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39

Page 48: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant expression

Compile time calculations are ill-formed.

int Two () {

return 2;

}

double array [Two () + 3]; // Ill -formed

The compiler is not aware of Two being constant.

We resolve this with constexpr.

constexpr int Two () {

return 2;

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39

Page 49: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

This can be applied to classes as well

struct Square {

explicit constexpr Square (unsigned side) :

_side (side)

{

}

constexpr unsigned Area () const {

return _side * _side;

}

void SetSide (unsigned side) {

_side = side;

}

private:

unsigned _side;

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 13 / 39

Page 50: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

This can be applied to classes as well

struct Square {

explicit constexpr Square (unsigned side) :

_side (side)

{

}

constexpr unsigned Area () const {

return _side * _side;

}

void SetSide (unsigned side) {

_side = side;

}

private:

unsigned _side;

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 13 / 39

Page 51: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

constexpr Square ces (3);

const Square cs (4);

Square s (5);

double cesArray [ces.Area ()];

// Error: cs.Area () is not constexpr

double csArray [ cs.Area ()];

// Error: s.Area () is not constexpr

double sArray [ s.Area ()];

ces.SetSide (6); // Error: this is Square const*

cs.SetSide (6); // Error: this is Square const*

s.SetSide (6);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39

Page 52: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

constexpr Square ces (3);

const Square cs (4);

Square s (5);

double cesArray [ces.Area ()];

// Error: cs.Area () is not constexpr

double csArray [ cs.Area ()];

// Error: s.Area () is not constexpr

double sArray [ s.Area ()];

ces.SetSide (6); // Error: this is Square const*

cs.SetSide (6); // Error: this is Square const*

s.SetSide (6);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39

Page 53: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

constexpr Square ces (3);

const Square cs (4);

Square s (5);

double cesArray [ces.Area ()];

// Error: cs.Area () is not constexpr

double csArray [ cs.Area ()];

// Error: s.Area () is not constexpr

double sArray [ s.Area ()];

ces.SetSide (6); // Error: this is Square const*

cs.SetSide (6); // Error: this is Square const*

s.SetSide (6);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39

Page 54: 20130110 prs presentation ncim c++ 11

Run-time performance enhancements Constant expression

Constant class expressions

constexpr Square ces (3);

const Square cs (4);

Square s (5);

double cesArray [ces.Area ()];

// Error: cs.Area () is not constexpr

double csArray [ cs.Area ()];

// Error: s.Area () is not constexpr

double sArray [ s.Area ()];

ces.SetSide (6); // Error: this is Square const*

cs.SetSide (6); // Error: this is Square const*

s.SetSide (6);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39

Page 55: 20130110 prs presentation ncim c++ 11

Usability enhancements

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancementsStatic assertionsType inferenceAnonymous functionsOverride and FinalAlias templatesEnumerations and Unions

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further reading

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 15 / 39

Page 56: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 57: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 58: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 59: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 60: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 61: 20130110 prs presentation ncim c++ 11

Usability enhancements Static assertions

Static assertions

We can perform compile time checks on constexpr.

constexpr bool b (false);

// Error: static assertion failed: b is false

static_assert (b, "b is false.");

constexpr Square ces (3);

static_assert (ces.Area () == 9, "Oops.");

This allows for static (unit) testing.

TMP and revised constexpr are Turing complete.

All unit tests can be static unit tests!

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39

Page 62: 20130110 prs presentation ncim c++ 11

Usability enhancements Type inference

Automatic type deduction

Inability to overload function based on return type opens up thepossibility for type inference.

Bonus: syntactic sugar

std::set <float >:: const_iterator b (s.begin ());

auto b (s.begin ());

Drawback: implicitness obfuscates bugs.

auto x (f ());

// Could be integer or float division.

// Even any matching division operator.

auto z = 1 / y;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39

Page 63: 20130110 prs presentation ncim c++ 11

Usability enhancements Type inference

Automatic type deduction

Inability to overload function based on return type opens up thepossibility for type inference.

Bonus: syntactic sugar

std::set <float >:: const_iterator b (s.begin ());

auto b (s.begin ());

Drawback: implicitness obfuscates bugs.

auto x (f ());

// Could be integer or float division.

// Even any matching division operator.

auto z = 1 / y;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39

Page 64: 20130110 prs presentation ncim c++ 11

Usability enhancements Type inference

Automatic type deduction

Inability to overload function based on return type opens up thepossibility for type inference.

Bonus: syntactic sugar

std::set <float >:: const_iterator b (s.begin ());

auto b (s.begin ());

Drawback: implicitness obfuscates bugs.

auto x (f ());

// Could be integer or float division.

// Even any matching division operator.

auto z = 1 / y;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39

Page 65: 20130110 prs presentation ncim c++ 11

Usability enhancements Type inference

Automatic type deduction

Inability to overload function based on return type opens up thepossibility for type inference.

Bonus: syntactic sugar

std::set <float >:: const_iterator b (s.begin ());

auto b (s.begin ());

Drawback: implicitness obfuscates bugs.

auto x (f ());

// Could be integer or float division.

// Even any matching division operator.

auto z = 1 / y;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39

Page 66: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Callbacks and closures

std::function makes callbacks more readable then conventionalfunction pointers.

#include <functional >

struct S {

int TimesTwo (short s) const {

return 2 * s;

}

};

// Using function pointers

typedef int (S::* Method) (short) const;

Method p (&S:: TimesTwo);

S const s;

int const x ((s.*p) (3)); // x == 6

// Using std:: function

std:: function <int (S const&, short)> q (&S:: TimesTwo);

int const y (q (s, 4)); // y == 8

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39

Page 67: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Callbacks and closures

std::function makes callbacks more readable then conventionalfunction pointers.

#include <functional >

struct S {

int TimesTwo (short s) const {

return 2 * s;

}

};

// Using function pointers

typedef int (S::* Method) (short) const;

Method p (&S:: TimesTwo);

S const s;

int const x ((s.*p) (3)); // x == 6

// Using std:: function

std:: function <int (S const&, short)> q (&S:: TimesTwo);

int const y (q (s, 4)); // y == 8

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39

Page 68: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Callbacks and closures

std::function makes callbacks more readable then conventionalfunction pointers.

#include <functional >

struct S {

int TimesTwo (short s) const {

return 2 * s;

}

};

// Using function pointers

typedef int (S::* Method) (short) const;

Method p (&S:: TimesTwo);

S const s;

int const x ((s.*p) (3)); // x == 6

// Using std:: function

std:: function <int (S const&, short)> q (&S:: TimesTwo);

int const y (q (s, 4)); // y == 8

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39

Page 69: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Callbacks and closures

std::function makes callbacks more readable then conventionalfunction pointers.

#include <functional >

struct S {

int TimesTwo (short s) const {

return 2 * s;

}

};

// Using function pointers

typedef int (S::* Method) (short) const;

Method p (&S:: TimesTwo);

S const s;

int const x ((s.*p) (3)); // x == 6

// Using std:: function

std:: function <int (S const&, short)> q (&S:: TimesTwo);

int const y (q (s, 4)); // y == 8

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39

Page 70: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Callbacks and closures

std::function makes callbacks more readable then conventionalfunction pointers.

#include <functional >

struct S {

int TimesTwo (short s) const {

return 2 * s;

}

};

// Using function pointers

typedef int (S::* Method) (short) const;

Method p (&S:: TimesTwo);

S const s;

int const x ((s.*p) (3)); // x == 6

// Using std:: function

std:: function <int (S const&, short)> q (&S:: TimesTwo);

int const y (q (s, 4)); // y == 8

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39

Page 71: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Lambda expressions

Syntax: [capture clause] (parameter list)-> returnvalue {body}

#include <functional >

double g (std:: function <double (int)> const& f) {

return f (4);

}

{

double x (2.);

std:: function <double (int)> const f (

[&x] (int n) -> double {

return x * n;

}

);

double const y (g (f)); // y == 8.

x = 3.;

double const z (g (f)); // z == 12.

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 19 / 39

Page 72: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Lambda expressions

Syntax: [capture clause] (parameter list)-> returnvalue {body}

#include <functional >

double g (std:: function <double (int)> const& f) {

return f (4);

}

{

double x (2.);

std:: function <double (int)> const f (

[&x] (int n) -> double {

return x * n;

}

);

double const y (g (f)); // y == 8.

x = 3.;

double const z (g (f)); // z == 12.

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 19 / 39

Page 73: 20130110 prs presentation ncim c++ 11

Usability enhancements Anonymous functions

Lambda expressions

Syntax: [capture clause] (parameter list)-> returnvalue {body}

#include <functional >

double g (std:: function <double (int)> const& f) {

return f (4);

}

{

double x (2.);

std:: function <double (int)> const f (

[&x] (int n) -> double {

return x * n;

}

);

double const y (g (f)); // y == 8.

x = 3.;

double const z (g (f)); // z == 12.

}

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 19 / 39

Page 74: 20130110 prs presentation ncim c++ 11

Usability enhancements Override and Final

Override and Final

Use override to avoid accidental creation of new virtual function.

Use final to prevent further specialization.

struct Interface {

virtual void f () = 0;

virtual void g () = 0;

};

struct Base : Interface {

virtual void f () override;

virtual void g () final;

};

struct final Derived : Base {

// Error: Signature of f doesn ’t match

virtual void f (int) override;

// Error: g finally overridden in Base

virtual void g () override;

};

// Error: Derived is final

struct Derived2 : Derived { ... };

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39

Page 75: 20130110 prs presentation ncim c++ 11

Usability enhancements Override and Final

Override and Final

Use override to avoid accidental creation of new virtual function.

Use final to prevent further specialization.

struct Interface {

virtual void f () = 0;

virtual void g () = 0;

};

struct Base : Interface {

virtual void f () override;

virtual void g () final;

};

struct final Derived : Base {

// Error: Signature of f doesn ’t match

virtual void f (int) override;

// Error: g finally overridden in Base

virtual void g () override;

};

// Error: Derived is final

struct Derived2 : Derived { ... };

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39

Page 76: 20130110 prs presentation ncim c++ 11

Usability enhancements Override and Final

Override and Final

Use override to avoid accidental creation of new virtual function.

Use final to prevent further specialization.

struct Interface {

virtual void f () = 0;

virtual void g () = 0;

};

struct Base : Interface {

virtual void f () override;

virtual void g () final;

};

struct final Derived : Base {

// Error: Signature of f doesn ’t match

virtual void f (int) override;

// Error: g finally overridden in Base

virtual void g () override;

};

// Error: Derived is final

struct Derived2 : Derived { ... };

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39

Page 77: 20130110 prs presentation ncim c++ 11

Usability enhancements Override and Final

Override and Final

Use override to avoid accidental creation of new virtual function.

Use final to prevent further specialization.

struct Interface {

virtual void f () = 0;

virtual void g () = 0;

};

struct Base : Interface {

virtual void f () override;

virtual void g () final;

};

struct final Derived : Base {

// Error: Signature of f doesn ’t match

virtual void f (int) override;

// Error: g finally overridden in Base

virtual void g () override;

};

// Error: Derived is final

struct Derived2 : Derived { ... };

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39

Page 78: 20130110 prs presentation ncim c++ 11

Usability enhancements Alias templates

Alias templates

Templated typedefs were illegal in C++03.

// Error: templated typedef not allowed.

template <typename T>

typedef std:: vector <T, std:: allocator <T> > Vector;

Possible workaround can lead to difficulties.

template <typename T>

class Vector :

public std:: vector <T, std:: allocator <T> > {};

C++11 allows us to solve it like this.

template <typename T>

using Vector = std:: vector <T, std:: allocator <T>>;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39

Page 79: 20130110 prs presentation ncim c++ 11

Usability enhancements Alias templates

Alias templates

Templated typedefs were illegal in C++03.

// Error: templated typedef not allowed.

template <typename T>

typedef std:: vector <T, std:: allocator <T> > Vector;

Possible workaround can lead to difficulties.

template <typename T>

class Vector :

public std:: vector <T, std:: allocator <T> > {};

C++11 allows us to solve it like this.

template <typename T>

using Vector = std:: vector <T, std:: allocator <T>>;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39

Page 80: 20130110 prs presentation ncim c++ 11

Usability enhancements Alias templates

Alias templates

Templated typedefs were illegal in C++03.

// Error: templated typedef not allowed.

template <typename T>

typedef std:: vector <T, std:: allocator <T> > Vector;

Possible workaround can lead to difficulties.

template <typename T>

class Vector :

public std:: vector <T, std:: allocator <T> > {};

C++11 allows us to solve it like this.

template <typename T>

using Vector = std:: vector <T, std:: allocator <T>>;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39

Page 81: 20130110 prs presentation ncim c++ 11

Usability enhancements Alias templates

Alias templates

Templated typedefs were illegal in C++03.

// Error: templated typedef not allowed.

template <typename T>

typedef std:: vector <T, std:: allocator <T> > Vector;

Possible workaround can lead to difficulties.

template <typename T>

class Vector :

public std:: vector <T, std:: allocator <T> > {};

C++11 allows us to solve it like this.

template <typename T>

using Vector = std:: vector <T, std:: allocator <T>>;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39

Page 82: 20130110 prs presentation ncim c++ 11

Usability enhancements Enumerations and Unions

Enumerations and Unions

Strongly typed enumerations Enumeration classes can no longer beimplicitly converted to int.

enum E; // Error: unknown underlying type

enum class E : unsigned long;

enum class E { A = 3, B, C };

Unrestricted unions Union members can be non-trivially constructibletypes, but the union constructor needs to be manuallydefined then.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39

Page 83: 20130110 prs presentation ncim c++ 11

Usability enhancements Enumerations and Unions

Enumerations and Unions

Strongly typed enumerations Enumeration classes can no longer beimplicitly converted to int.

enum E; // Error: unknown underlying type

enum class E : unsigned long;

enum class E { A = 3, B, C };

Unrestricted unions Union members can be non-trivially constructibletypes, but the union constructor needs to be manuallydefined then.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39

Page 84: 20130110 prs presentation ncim c++ 11

Usability enhancements Enumerations and Unions

Enumerations and Unions

Strongly typed enumerations Enumeration classes can no longer beimplicitly converted to int.

enum E; // Error: unknown underlying type

enum class E : unsigned long;

enum class E { A = 3, B, C };

Unrestricted unions Union members can be non-trivially constructibletypes, but the union constructor needs to be manuallydefined then.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39

Page 85: 20130110 prs presentation ncim c++ 11

New functionality

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancements

5 New functionalityVariadic templatesDefaulted and deleted special methods

6 Standard library enhancements

7 Compiler support

8 Further readingRalph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 23 / 39

Page 86: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Templated types can have a variadic template parameter list.

Unlike variadic macros and functions, variadic templates are type-safe.

template <typename T, typename ... O>

struct Tuple {

T _value;

Tuple <O...> _others;

explicit Tuple (T value , O... others) :

_value (value),

_others (others ...)

{

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39

Page 87: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Templated types can have a variadic template parameter list.

Unlike variadic macros and functions, variadic templates are type-safe.

template <typename T, typename ... O>

struct Tuple {

T _value;

Tuple <O...> _others;

explicit Tuple (T value , O... others) :

_value (value),

_others (others ...)

{

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39

Page 88: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Templated types can have a variadic template parameter list.

Unlike variadic macros and functions, variadic templates are type-safe.

template <typename T, typename ... O>

struct Tuple {

T _value;

Tuple <O...> _others;

explicit Tuple (T value , O... others) :

_value (value),

_others (others ...)

{

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39

Page 89: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Templated types can have a variadic template parameter list.

Unlike variadic macros and functions, variadic templates are type-safe.

template <typename T, typename ... O>

struct Tuple {

T _value;

Tuple <O...> _others;

explicit Tuple (T value , O... others) :

_value (value),

_others (others ...)

{

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39

Page 90: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Specialization:

template <typename T>

struct Tuple <T> {

T _value;

explicit Tuple (T value) :

_value (value)

{

}

};

Usage:

Tuple <int , double , bool > tuple (3, 2., true);

tuple._value; // == 3

tuple._others._value; // == 2.

tuple._others._others._value; // == true

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 25 / 39

Page 91: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

Variadic templates

Specialization:

template <typename T>

struct Tuple <T> {

T _value;

explicit Tuple (T value) :

_value (value)

{

}

};

Usage:

Tuple <int , double , bool > tuple (3, 2., true);

tuple._value; // == 3

tuple._others._value; // == 2.

tuple._others._others._value; // == true

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 25 / 39

Page 92: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

std::tuple

Better version provided by STL.

#include <tuple >

std:: tuple <int , double > idTuple (3, 5.);

std::get <1> (idTuple); // == 5.

std:: tuple <int , bool > ibTuple (4, false);

idTuple = ibTuple; // Error: different tuple types.

bool inserted;

std::set <int >:: iterator i;

std::set <int > s;

std::tie (i, inserted) = s.insert (3);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39

Page 93: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

std::tuple

Better version provided by STL.

#include <tuple >

std:: tuple <int , double > idTuple (3, 5.);

std::get <1> (idTuple); // == 5.

std:: tuple <int , bool > ibTuple (4, false);

idTuple = ibTuple; // Error: different tuple types.

bool inserted;

std::set <int >:: iterator i;

std::set <int > s;

std::tie (i, inserted) = s.insert (3);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39

Page 94: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

std::tuple

Better version provided by STL.

#include <tuple >

std:: tuple <int , double > idTuple (3, 5.);

std::get <1> (idTuple); // == 5.

std:: tuple <int , bool > ibTuple (4, false);

idTuple = ibTuple; // Error: different tuple types.

bool inserted;

std::set <int >:: iterator i;

std::set <int > s;

std::tie (i, inserted) = s.insert (3);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39

Page 95: 20130110 prs presentation ncim c++ 11

New functionality Variadic templates

std::tuple

Better version provided by STL.

#include <tuple >

std:: tuple <int , double > idTuple (3, 5.);

std::get <1> (idTuple); // == 5.

std:: tuple <int , bool > ibTuple (4, false);

idTuple = ibTuple; // Error: different tuple types.

bool inserted;

std::set <int >:: iterator i;

std::set <int > s;

std::tie (i, inserted) = s.insert (3);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39

Page 96: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Defaulted special methods

Definition like class C { int i; }; defaults to

class C {

int i;

public:

C () {} // Empty constructor

C (C const& c) : i (c.i) {} // Copy constructor

~C () {} // Default destructor

C& operator= (C const& c) { // Assignment operator

if (this != &c) {

i = c.i;

}

return *this;

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 27 / 39

Page 97: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Defaulted special methods

Definition like class C { int i; }; defaults to

class C {

int i;

public:

C () {} // Empty constructor

C (C const& c) : i (c.i) {} // Copy constructor

~C () {} // Default destructor

C& operator= (C const& c) { // Assignment operator

if (this != &c) {

i = c.i;

}

return *this;

}

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 27 / 39

Page 98: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Default and Delete

C++11 allows control over these default methods.

class C {

int i;

public:

// Don’t allow implicit empty construction

C () = delete;

// Generate default implicit copy constructor

C (C const &) = default;

// Destructor and assignment operator left

// implicitly default.

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 28 / 39

Page 99: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Default and Delete

C++11 allows control over these default methods.

class C {

int i;

public:

// Don’t allow implicit empty construction

C () = delete;

// Generate default implicit copy constructor

C (C const &) = default;

// Destructor and assignment operator left

// implicitly default.

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 28 / 39

Page 100: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Default and Delete

C++11 allows control over these default methods.

class C {

int i;

public:

// Don’t allow implicit empty construction

C () = delete;

// Generate default implicit copy constructor

C (C const &) = default;

// Destructor and assignment operator left

// implicitly default.

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 28 / 39

Page 101: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Prohibit method calls

delete can also be used to prohibit calling methods with specificarguments.

struct S {

void f (double d);

// Prohibit calling f with float argument

void f (float) = delete;

// Prohibit calling f with any argument

// except double

template <typename T> void f (T) = delete;

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 29 / 39

Page 102: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Prohibit method calls

delete can also be used to prohibit calling methods with specificarguments.

struct S {

void f (double d);

// Prohibit calling f with float argument

void f (float) = delete;

// Prohibit calling f with any argument

// except double

template <typename T> void f (T) = delete;

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 29 / 39

Page 103: 20130110 prs presentation ncim c++ 11

New functionality Defaulted and deleted special methods

Prohibit method calls

delete can also be used to prohibit calling methods with specificarguments.

struct S {

void f (double d);

// Prohibit calling f with float argument

void f (float) = delete;

// Prohibit calling f with any argument

// except double

template <typename T> void f (T) = delete;

};

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 29 / 39

Page 104: 20130110 prs presentation ncim c++ 11

Standard library enhancements

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancements

5 New functionality

6 Standard library enhancementsThreading facilitiesSmart pointersType traits

7 Compiler support

8 Further reading

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 30 / 39

Page 105: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 106: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 107: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 108: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 109: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 110: 20130110 prs presentation ncim c++ 11

Standard library enhancements Threading facilities

Threading facilities

C++11 provides platform independent threading facilities.

#include <thread >

auto f ([] (int& out , short in) { out = 2*in; });

int result (0);

std:: thread t (f, std::ref (result), 3);

t.join ();

// result == 6

They’re accompanied by objects like mutexes, conditional variablesand RAII locks.

Mutexes can be avoided by using atomic operations and memorybarriers.

Asynchronous thread communication is facilitated by futures andpromises.

Thread pools are planned for upcoming C++ standards.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39

Page 111: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Memory leaks

How to avoid memory leaks?

int* i (new int (0));

f (i); // Could throw

delete i; // Might be too late

Cumbersome alternative.

int* i (new int (1));

try {

f (i);

} catch (fException const& e) { ... }

delete i;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 32 / 39

Page 112: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Memory leaks

How to avoid memory leaks?

int* i (new int (0));

f (i); // Could throw

delete i; // Might be too late

Cumbersome alternative.

int* i (new int (1));

try {

f (i);

} catch (fException const& e) { ... }

delete i;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 32 / 39

Page 113: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Memory leaks

How to avoid memory leaks?

int* i (new int (0));

f (i); // Could throw

delete i; // Might be too late

Cumbersome alternative.

int* i (new int (1));

try {

f (i);

} catch (fException const& e) { ... }

delete i;

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 32 / 39

Page 114: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

RAII as a solution

The problem doesn’t occur on the stack

int i;

f (&i); // May throw; i is cleaned up

Solution: Make a stack object responsible for deprecation ofdynamically allocated space.

RAII: Resource Acquisition Is Initialization.

C++11 improves on smart pointers implementing RAII.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39

Page 115: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

RAII as a solution

The problem doesn’t occur on the stack

int i;

f (&i); // May throw; i is cleaned up

Solution: Make a stack object responsible for deprecation ofdynamically allocated space.

RAII: Resource Acquisition Is Initialization.

C++11 improves on smart pointers implementing RAII.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39

Page 116: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

RAII as a solution

The problem doesn’t occur on the stack

int i;

f (&i); // May throw; i is cleaned up

Solution: Make a stack object responsible for deprecation ofdynamically allocated space.

RAII: Resource Acquisition Is Initialization.

C++11 improves on smart pointers implementing RAII.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39

Page 117: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

RAII as a solution

The problem doesn’t occur on the stack

int i;

f (&i); // May throw; i is cleaned up

Solution: Make a stack object responsible for deprecation ofdynamically allocated space.

RAII: Resource Acquisition Is Initialization.

C++11 improves on smart pointers implementing RAII.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39

Page 118: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

RAII as a solution

The problem doesn’t occur on the stack

int i;

f (&i); // May throw; i is cleaned up

Solution: Make a stack object responsible for deprecation ofdynamically allocated space.

RAII: Resource Acquisition Is Initialization.

C++11 improves on smart pointers implementing RAII.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39

Page 119: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Smart pointers

std::unique_ptr deprecates std::auto_ptr.

#include <memory >

std:: unique_ptr <int > i (new int (0));

// Error: no copy constructor

std:: unique_ptr <int > j (i);

Joint ownership is facilitated by std::shared_ptr and std::weak_ptr.

std:: shared_ptr <int > i (new int (1));

std:: shared_ptr <int > j (i); // Joint ownership

// Doesn ’t contribute to reference counting

std:: weak_ptr <int > w (i);

std:: shared_ptr <int > k (w);

Behaviour is similar to a real pointer. e.g. casting:

std:: shared_ptr <Derived > d (new Derived ());

std:: shared_ptr <Base > b (

std:: dynamic_pointer_cast <Base , Derived > (d));

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39

Page 120: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Smart pointers

std::unique_ptr deprecates std::auto_ptr.

#include <memory >

std:: unique_ptr <int > i (new int (0));

// Error: no copy constructor

std:: unique_ptr <int > j (i);

Joint ownership is facilitated by std::shared_ptr and std::weak_ptr.

std:: shared_ptr <int > i (new int (1));

std:: shared_ptr <int > j (i); // Joint ownership

// Doesn ’t contribute to reference counting

std:: weak_ptr <int > w (i);

std:: shared_ptr <int > k (w);

Behaviour is similar to a real pointer. e.g. casting:

std:: shared_ptr <Derived > d (new Derived ());

std:: shared_ptr <Base > b (

std:: dynamic_pointer_cast <Base , Derived > (d));

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39

Page 121: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Smart pointers

std::unique_ptr deprecates std::auto_ptr.

#include <memory >

std:: unique_ptr <int > i (new int (0));

// Error: no copy constructor

std:: unique_ptr <int > j (i);

Joint ownership is facilitated by std::shared_ptr and std::weak_ptr.

std:: shared_ptr <int > i (new int (1));

std:: shared_ptr <int > j (i); // Joint ownership

// Doesn ’t contribute to reference counting

std:: weak_ptr <int > w (i);

std:: shared_ptr <int > k (w);

Behaviour is similar to a real pointer. e.g. casting:

std:: shared_ptr <Derived > d (new Derived ());

std:: shared_ptr <Base > b (

std:: dynamic_pointer_cast <Base , Derived > (d));

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39

Page 122: 20130110 prs presentation ncim c++ 11

Standard library enhancements Smart pointers

Smart pointers

std::unique_ptr deprecates std::auto_ptr.

#include <memory >

std:: unique_ptr <int > i (new int (0));

// Error: no copy constructor

std:: unique_ptr <int > j (i);

Joint ownership is facilitated by std::shared_ptr and std::weak_ptr.

std:: shared_ptr <int > i (new int (1));

std:: shared_ptr <int > j (i); // Joint ownership

// Doesn ’t contribute to reference counting

std:: weak_ptr <int > w (i);

std:: shared_ptr <int > k (w);

Behaviour is similar to a real pointer. e.g. casting:

std:: shared_ptr <Derived > d (new Derived ());

std:: shared_ptr <Base > b (

std:: dynamic_pointer_cast <Base , Derived > (d));

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39

Page 123: 20130110 prs presentation ncim c++ 11

Standard library enhancements Type traits

Type traits for TMP

C++11 elaborates on type computation and transformation atcompile time.

struct Base {}; struct Derived : Base {};

static_assert (std:: is_base_of <Base , Derived >::value ,

"Oops");

template <typename T> struct Algorithm {

static int Execute (typename std:: conditional <

std:: is_arithmetic <T>::value , T,

typename std:: add_lvalue_reference <T>:: type

>::type

);

};

//int Execute (int);

Algorithm <int >:: Execute (5);

//int Execute (std:: vector <int >&);

Algorithm <std:: vector <int >>:: Execute (v);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39

Page 124: 20130110 prs presentation ncim c++ 11

Standard library enhancements Type traits

Type traits for TMP

C++11 elaborates on type computation and transformation atcompile time.

struct Base {}; struct Derived : Base {};

static_assert (std:: is_base_of <Base , Derived >::value ,

"Oops");

template <typename T> struct Algorithm {

static int Execute (typename std:: conditional <

std:: is_arithmetic <T>::value , T,

typename std:: add_lvalue_reference <T>:: type

>::type

);

};

//int Execute (int);

Algorithm <int >:: Execute (5);

//int Execute (std:: vector <int >&);

Algorithm <std:: vector <int >>:: Execute (v);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39

Page 125: 20130110 prs presentation ncim c++ 11

Standard library enhancements Type traits

Type traits for TMP

C++11 elaborates on type computation and transformation atcompile time.

struct Base {}; struct Derived : Base {};

static_assert (std:: is_base_of <Base , Derived >::value ,

"Oops");

template <typename T> struct Algorithm {

static int Execute (typename std:: conditional <

std:: is_arithmetic <T>::value , T,

typename std:: add_lvalue_reference <T>:: type

>::type

);

};

//int Execute (int);

Algorithm <int >:: Execute (5);

//int Execute (std:: vector <int >&);

Algorithm <std:: vector <int >>:: Execute (v);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39

Page 126: 20130110 prs presentation ncim c++ 11

Standard library enhancements Type traits

Type traits for TMP

C++11 elaborates on type computation and transformation atcompile time.

struct Base {}; struct Derived : Base {};

static_assert (std:: is_base_of <Base , Derived >::value ,

"Oops");

template <typename T> struct Algorithm {

static int Execute (typename std:: conditional <

std:: is_arithmetic <T>::value , T,

typename std:: add_lvalue_reference <T>:: type

>::type

);

};

//int Execute (int);

Algorithm <int >:: Execute (5);

//int Execute (std:: vector <int >&);

Algorithm <std:: vector <int >>:: Execute (v);

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39

Page 127: 20130110 prs presentation ncim c++ 11

Compiler support

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancements

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further reading

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 36 / 39

Page 128: 20130110 prs presentation ncim c++ 11

Compiler support

Compiler support

C++11 covers about 40 subjects.

MSVC 2012 supports about half of them, while GCC 4.8 and CLang3.1 are almost feature complete.

Enabling C++ 11 is automatic for MSVC, but requires an additionalcommandline option -std=c++11 for GCC and CLang.

TR2 is already being implemented in GCC.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39

Page 129: 20130110 prs presentation ncim c++ 11

Compiler support

Compiler support

C++11 covers about 40 subjects.

MSVC 2012 supports about half of them, while GCC 4.8 and CLang3.1 are almost feature complete.

Enabling C++ 11 is automatic for MSVC, but requires an additionalcommandline option -std=c++11 for GCC and CLang.

TR2 is already being implemented in GCC.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39

Page 130: 20130110 prs presentation ncim c++ 11

Compiler support

Compiler support

C++11 covers about 40 subjects.

MSVC 2012 supports about half of them, while GCC 4.8 and CLang3.1 are almost feature complete.

Enabling C++ 11 is automatic for MSVC, but requires an additionalcommandline option -std=c++11 for GCC and CLang.

TR2 is already being implemented in GCC.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39

Page 131: 20130110 prs presentation ncim c++ 11

Compiler support

Compiler support

C++11 covers about 40 subjects.

MSVC 2012 supports about half of them, while GCC 4.8 and CLang3.1 are almost feature complete.

Enabling C++ 11 is automatic for MSVC, but requires an additionalcommandline option -std=c++11 for GCC and CLang.

TR2 is already being implemented in GCC.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39

Page 132: 20130110 prs presentation ncim c++ 11

Compiler support

Compiler support

C++11 covers about 40 subjects.

MSVC 2012 supports about half of them, while GCC 4.8 and CLang3.1 are almost feature complete.

Enabling C++ 11 is automatic for MSVC, but requires an additionalcommandline option -std=c++11 for GCC and CLang.

TR2 is already being implemented in GCC.

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39

Page 133: 20130110 prs presentation ncim c++ 11

Further reading

Outline

1 Timeline

2 C++11 Development

3 Run-time performance enhancements

4 Usability enhancements

5 New functionality

6 Standard library enhancements

7 Compiler support

8 Further reading

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 38 / 39

Page 134: 20130110 prs presentation ncim c++ 11

Further reading

Further reading

Thank you for your attention.

Some resources for further reading.

C++ Reference

ISO C++

Boost

C++ 11 Compiler support

C++ 11 Wiki

TR1 and TR2

Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 39 / 39