C++11 and Why You Care Ugo Jardonnet
August 26, 2012
Ugo Jardonnet
C++11 and Why You Care
1 / 22
Table of Contents 1
Introduction
2
Performance and Control Lambda: Control Closure Capture Control Compile-Time vs Run-Time execution Control Memory Management
3
Productivity Oriented Languages
4
New language features and librabries C++11 feels like ... User-defined Literal Variadic templates Threading
Ugo Jardonnet
C++11 and Why You Care
2 / 22
Bootstrap
Bootstrap
The C++ programming language:
1990
1995
2000
+ C+ ne w : 20 11
20
03
:
Th e
In te rn a
da rd iza St an : 98 19
1985
tio na l
tio n
gu la n
cla ss
e
wi th
Th
C
:
:
83
79
19
19
1980
C+ +
es
ag
e
St an da rd
”is a super-set of C with stronger abstraction” [11]
2005
2010
The C++ programming language follows the zero-overhead principle [7]
Ugo Jardonnet
C++11 and Why You Care
3 / 22
Introduction
Outline 1
Introduction
2
Performance and Control Lambda: Control Closure Capture Control Compile-Time vs Run-Time execution Control Memory Management
3
Productivity Oriented Languages
4
New language features and librabries C++11 feels like ... User-defined Literal Variadic templates Threading
Ugo Jardonnet
C++11 and Why You Care
4 / 22
Introduction
Introduction
What makes C++11 ”feels like a new language”, Stroustrup [8] ? auto, lambda, range based for, ... Do we still need a language like C++? C# and Java focus on productivity C++ is about performance and control ... also the world is built on C/C++: C#, Java, Javascript, Ruby, Python, ... Windows Core functions [11] Visual Studio [3]
Ugo Jardonnet
C++11 and Why You Care
5 / 22
Performance and Control
Outline 1
Introduction
2
Performance and Control Lambda: Control Closure Capture Control Compile-Time vs Run-Time execution Control Memory Management
3
Productivity Oriented Languages
4
New language features and librabries C++11 feels like ... User-defined Literal Variadic templates Threading
Ugo Jardonnet
C++11 and Why You Care
6 / 22
Performance and Control
Lambda: Control Closure Capture
C++ Lambda
Definition [capture](parameters) -> return-type {body} Example 1 2 3
1
std :: sort ( begin ( v ) , end ( v ) , []( const T & a , const T & b ) { return a . x < b . x ; }) ;
1 [](auto
e) is not valid in C++ nor any kind of variadic lambda
Ugo Jardonnet
C++11 and Why You Care
7 / 22
Performance and Control
Lambda: Control Closure Capture
C++ Lambda : Control Closure Capture A closure is a function capturing free variables in the lexical environment Lambda Closure 1 2 3 4 5 6
[] // The use any external variables is an error . [x , & y ] // x is captured by value , y is captured by reference [&] // Implicitly capture by reference [=] // Implicitly capture by value [& , x ] // x is explicitly captured by value . Other variables will be captured by reference [= , & z ] // z is explicitly captured by reference . Other variables will be captured by value
Question: In C#, how do lambdas capture variables of their closure ? Python ? Java ?
Ugo Jardonnet
C++11 and Why You Care
8 / 22
Performance and Control
Control Compile-Time vs Run-Time execution
Control Compile-Time vs Run-Time execution Compile-Time Execution constexpr 1 2 3
2
constexpr factorial ( int n ) { return n > 0 ? n * factorial ( n - 1 ) : 1; }
static assert 1 2 3 4 5
template < typename T > struct Check { static_assert ( sizeof ( int ) decltype ( a * b ) { /* ... */ };
1 2 3 4 5 6 7
template < typename T > typename enable_if < is_integral :: value , void >:: type f ( T ) { cout :: type f ( T ) { cout requires Integral f ( T ) { /* ... */ }
but are more like this 1 template < typename T > 2 typename enable_if < is_integral :: value , void >:: type 3 f ( T ) { /* ... */ }
though can be more something like this [2] 1 template < typename T , EnableIf < is_integral >... > 2 f () { /* ... */ }
Ugo Jardonnet
C++11 and Why You Care
11 / 22
Performance and Control
Control Memory Management
Control Memory Management Fine grain memory management through Smart Pointers
4
unique ptr 1 std :: unique_ptr < int > p1 ( new int (5) ) ; 2 std :: unique_ptr < int > p2 = p1 ; // Compile error . 3 std :: unique_ptr < int > p3 = std :: move ( p1 ) ; // Transfers ownership . p3 now owns the memory and p1 is rendered invalid .
shared ptr 1 std :: shared_ptr < int > p1 ( new int (5) ) ; 2 std :: shared_ptr < int > p2 = p1 ; // Both now own the memory .
weak ptr 1 // care if you have cyclic references ...
4 Java
Dispose, C# using, D scope, ...
Ugo Jardonnet
C++11 and Why You Care
12 / 22
Productivity Oriented Languages
Outline 1
Introduction
2
Performance and Control Lambda: Control Closure Capture Control Compile-Time vs Run-Time execution Control Memory Management
3
Productivity Oriented Languages
4
New language features and librabries C++11 feels like ... User-defined Literal Variadic templates Threading
Ugo Jardonnet
C++11 and Why You Care
13 / 22
Productivity Oriented Languages
Productivity Oriented Languages Productivity Virtual Machine Garbage Collection MetaData linq
Control Cache locality
4
and Memory Alignment ?
Optimization completely relies on JIT compiler
5
jdk7/mytl/jdk/src/share/classes/sun/tools/javac/Main.java
6
1 if ( argv [ i ]. equals ( " -O " ) ) { 2 // -O is accepted for backward compatibility , but 3 // is no longer effective . [...] 4 } 4 L1
Cache ref is 0.5ns, Main Memory ref 100ns, disk seek 10,000,000ns [6] “which is in the business of being fast” [9]. 6 See also Devirtualizing method calls in Java [5] 5 JIT
Ugo Jardonnet
C++11 and Why You Care
14 / 22
Productivity Oriented Languages
Productivity Oriented Languages
Performance JIT compilers are getting better. C++ compilers are getting better. NextGen/AOT compilers are coming. ... in fact the trend is Going Native [4]. By the way ... Linq is a nice tool but also a performance killer 5 . Access to MetaData is even slower than people say (Enum.ToString?).
5 Enumerable.Cast: Ugo Jardonnet
Top 5 most consuming function of Web.Widget C++11 and Why You Care
15 / 22
New language features and librabries
Outline 1
Introduction
2
Performance and Control Lambda: Control Closure Capture Control Compile-Time vs Run-Time execution Control Memory Management
3
Productivity Oriented Languages
4
New language features and librabries C++11 feels like ... User-defined Literal Variadic templates Threading
Ugo Jardonnet
C++11 and Why You Care
16 / 22
New language features and librabries
C++11 feels like ...
... in fact C++11 feels like Python python3 1 2 3 4 5
v = [1 ,2 ,3 ,4 ,5] for e in v : print ( " - " , e ) unitlist = [ cross ( rs , cs ) for rs in [ ’ ABC ’ , ’ DEF ’ , ’ GHI ’] \ for cs in [ ’ 123 ’ , ’ 456 ’ ]]
C++11 1 2 3 4 5 6 7 8 9
auto v = {1 ,2 ,3 ,4 ,5}; for ( auto e : v ) println ( " - " , e ) ;
// initializer list
vector < vector < string > > unitlist ; for ( auto rs : { " ABC " _vec , " DEF " _vec , " GHI " _vec }) { for ( auto cs : { " 123 " _vec , " 456 " _vec }) { unitlist . push_back ( cross ( rs , cs ) ) ; } }
Ugo Jardonnet
C++11 and Why You Care
17 / 22
New language features and librabries
User-defined Literal
User-defined Literal "" vec 1 2 3 4 5 6 7 8 9
constexpr vector < string > operator " " _vec ( char const * str , size_t N ) { vector < string > output ; for_each ( str , str +N , [= , & output ] ( char c ) { output . push_back ( string (1 , c ) ) ; }) ; return output ; }
Good or Bad (?), finally we’ve got std::string literal !! 1 2 3
std :: string operator " " _s ( const char * str , size_t len ) { return std :: string { str , len }; }
Ugo Jardonnet
C++11 and Why You Care
18 / 22
New language features and librabries
Variadic templates
Variadic templates Variadic templates are a type-safe1 alternative to vargars and much more. println 1 2 3 4 5 6 7 8
void println () { std :: cout void println ( const T & value , const Args &... args ) { std :: cout v . push_back ( async ([] { v . push_back ( async ([] { v . push_back ( async ([] {
v; return flip ( " , olleH " ) ; }) ) ; return flip ( " egdelwonK " ) ; }) ) ; return flip ( " \ n ! rebmahc " ) ; }) ) ;
for ( auto & e : v ) { cout