Applied C++11 2013-01-25 Fri
Outline Introduction Auto-Type Inference Lambda Functions Threading Compiling
C++11 C++11 (formerly known as C++0x) is the most recent version of the standard of the C++ Approved by ISO(International Organization for Standardization) on 12 August 2011 Includes several additions to the core language and extends the C++ standard library
Core language usability enhancements: Primary purpose is to make the language easier to use Improve type safety Minimize code repetition Make erroneous code less likely
Auto Type Inference The compiler can infer the type of a variable at the point of declaration: i n t x = 5 ; auto x = 4 ; Very helpful when working with templates and iterators: v e c t o r <i n t > vec ; auto i t r = vec. i t e r a t o r ( ) ; // i n s t e a d o f v e c t o r <i n t > : : i t e r a t o r i t r
Study Case t e m p l a t e <typename BuiltType, typename B u i l d e r > void makeandprocessobject ( const B u i l d e r& b u i l d e r ) { B u i l t T y p e v a l = b u i l d e r. makeobject ( ) ; // do s t u f f w i t h v a l two necessary template parameters type of the builder object type of the object being built MyObjBuilder b u i l d e r ; makeandprocessobject<myobj>( b u i l d e r ) ;
Study Case (cont.) By using `auto' no longer need to write the specic type of the built object t e m p l a t e <typename B u i l d e r > void makeandprocessobject ( const B u i l d e r& b u i l d e r ) { auto v a l = b u i l d e r. makeobject ( ) ; // do s t u f f w i t h v a l MyObjBuilder b u i l d e r ; makeandprocessobject ( b u i l d e r ) ;
Lambda Functions Lambda Function: is a function dened, and possibly called, without being bound to an identier you can write inline in your source code convenient to pass as an argument to a higher-order function allow to create entirely new ways of writing programs
Basic Lambda Syntax #i n c l u d e <i o s t r e a m > u s i n g namespace s t d ; i n t main ( ) { [ ] ( ) { cout << " H e l l o, World! " ; ( ) ; []: capture specier, species for the compiler creation of a lambda function (): argument list What about return values?
More on the Lambda Syntax If there is no return statement, uses void as default If you have a simple return expression, the compiler will deduce the type of the return value // c o m p i l e r knows t h i s r e t u r n s an i n t e g e r [ ] ( ) { r e t u r n 1 ; specify the return value explicitly // now we ' r e t e l l i n g t h e c o m p i l e r what we want [ ] ( ) > i n t { r e t u r n 1 ;
Example: Sorting a list We create a list of cities. // D e f i n e a c l a s s f o r a c i t y c l a s s C i t y { p u b l i c : i n t p o p u l a t i o n ; i n t f o u n d a t i o n _ y e a r ; i n t s u r f a c e _ a r e a ; ; i n t main ( ) { s t d : : l i s t <C i t y > c i t i e s ; // F i l l t h e l i s t w i t h d a t a To sort a set of objects, we have to dene a comparison function.
Example: Sorting a list (cont.) In the STL, comparison functions have two arguments, and return true if the rst argument goes before the second. To sort by population, we can dene a compare function: b o o l compare ( C i t y a, C i t y b ) { r e t u r n a. p o p u l a t i o n < b. p o p u l a t i o n ;... c i t i e s. s o r t ( compare ) ; Alternatively, we can use a lambda function: c i t i e s. s o r t ( [ ] ( C i t y a, C i t y b) >b o o l { r e t u r n a. p u p u l a t i o n < b. p o p u l a t i o n ; ) ;
Multithreading C++11 threads are more convenient than directly using Posix threads. #i n c l u d e <t h r e a d > i n t main ( ) {... // D e c l a r i n g t h r e a d s s t d : : t h r e a d t ; // A s s i g n a f u n c t i o n t = s t d : : t h r e a d ( f u n c t i o n, a r g u m e n t s ) ;... // F i n a l i z i n g t. j o i n ( ) ; Compile with the ag -pthread
Multithreading example: Dot product // D e f i n i t i o n o f a c l a s s t o s t o r e t h e v a l u e s c l a s s P a r t i a l D P { p u b l i c : double d o t p r o d u c t ; double * x ; double * y ; i n t f i r s t ; // F i r s t i n d e x o f a r r a y i n t l e n g t h ; // Count o f e l e m e n t s P a r t i a l D P ( ) : d o t p r o d u c t ( 0 ) { s e t ( double * x i n, double * y i n, i n t f, i n t l ) { x = x i n ; y = y i n ; f i r s t = f ; l e n g t h = l ; ; o p e r a t o r ( ) ( ) { f o r ( i n t i = f i r s t ; i < f i r s t + l e n g t h ; i ++) { d o t p r o d u c t += x [ i ] + y [ i ] ;
Multithreading example (cont.) #i n c l u d e <t h r e a d > const i n t s i z e = 1 0 0 0, n t h r e a d s = 4 ; i n t main ( ) { s t d : : t h r e a d t [ n t h r e a d s ] ; double x [ s i z e ], y [ s i z e ] ; i n t s u b s i z e = s i z e / n t h r e a d s ; // Assume no r e m a i n d e r P a r t i a l D P dp [ n t h r e a d s ] ; f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++){ dp [ i ]. s e t ( x, y, i * s u b s i z e, s u b s i z e ) ; t [ i ] = s t d : : t h r e a d ( dp [ i ] ) ; // Compute f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++) t [ i ]. j o i n ( ) ; // A c cumulate t h e p r o d u c t double r e s u l t = 0 ; f o r ( i n t i = 0 ; i < n t h r e a d s ; i ++) r e s u l t += dp [ i ]. d o t p r o d u c t ;
Compiling C++11 With gcc, C++11 features can be compiled by using g++ -std=c++11 <args>