C++ 11 Ralph Langendam NCIM-Groep Januari 9th, 2013 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 1 / 39
May 10, 2015
C++ 11
Ralph Langendam
NCIM-Groep
Januari 9th, 2013
Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 1 / 39
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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