SlideShare une entreprise Scribd logo
1  sur  134
Télécharger pour lire hors ligne
C++ 11

                               Ralph Langendam

                                  NCIM-Groep


                               Januari 9th, 2013




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

                                          Anonymous functions
1   Timeline
                                          Override and Final
      History
                                          Alias templates
      Present and Future
                                          Enumerations and Unions
2   C++11 Development              5    New functionality
      Directives                          Variadic templates
      Enhancement categories              Defaulted and deleted special
3   Run-time performance                  methods
    enhancements                   6    Standard library enhancements
      Move semantics                      Threading facilities
      Constant expression                 Smart pointers
4   Usability enhancements                Type traits
      Static assertions            7    Compiler support
      Type inference               8    Further reading

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


Outline

1     Timeline
        History
        Present and Future

2     C++11 Development

3     Run-time performance enhancements

4     Usability enhancements

5     New functionality

6     Standard library enhancements

7     Compiler support

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


History




 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.




 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 (//).




 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++ Programming
               Language.




 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++ Programming
               Language.
          1989 protected and static members.




 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++ Programming
               Language.
          1989 protected and static members.
          1990 Publication of The Annotated C++ Reference Manual,
               Borland Turbo C++.




 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++ Programming
               Language.
          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.




 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++ Programming
               Language.
          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



 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++ Programming
               Language.
          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.


 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++ Programming
               Language.
          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




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


Present and Future




 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.




 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.




 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.




 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 Development
       Directives
       Enhancement categories

3     Run-time performance enhancements

4     Usability enhancements

5     New functionality

6     Standard library enhancements

7     Compiler support

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


Directives




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


Directives



      Backwards compatibility with C++98 and C.




 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.




 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.




 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.




 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.




 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.




 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




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


Enhancement categories




      Run-time performance 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




 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




 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




 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 enhancements
        Move semantics
        Constant expression

4     Usability enhancements

5     New functionality

6     Standard library enhancements

7     Compiler support

    Ralph Langendam (NCIM-Groep)                      C++ 11   Januari 9th, 2013   9 / 39
Run-time performance enhancements   Move semantics


R-value references




 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




 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) ) ;




 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.




 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




 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;
      }




 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.




 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




 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




 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.




 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




 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




 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) ;




 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 () ];




 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 enhancements
      Static assertions
      Type inference
      Anonymous functions
      Override and Final
      Alias templates
      Enumerations and Unions

5   New functionality

6 Ralph Langendam library enhancements
     Standard (NCIM-Groep)                     C++ 11   Januari 9th, 2013   15 / 39
Usability enhancements   Static assertions


Static assertions




 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 . " ) ;




 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 . " ) ;




 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.




 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 T¨ring complete.
                                     u




 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 T¨ring complete.
                                     u
      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




 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 the
      possibility for type inference.




 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 the
      possibility for type inference.
      Bonus: syntactic sugar
      std :: set < float >:: const_iterator b ( s . begin () ) ;
                                       auto b ( s . begin () ) ;




 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 the
      possibility 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




 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 conventional
      function pointers.




 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 conventional
      function pointers.
      # include < functional >
      struct S {
           int TimesTwo ( short s ) const {
               return 2 * s ;
           }
      };




 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 conventional
      function 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




 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 conventional
      function 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




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


Lambda expressions

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




 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




 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.




 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.




 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




 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 ;




 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 > > {};




 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




 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 be
             implicitly converted to int.
                   enum E ; // Error : unknown underlying type
                   enum class E : unsigned long ;
                   enum class E { A = 3 , B , C };




 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 be
             implicitly 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 constructible
              types, but the union constructor needs to be manually
              defined 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 functionality
        Variadic templates
        Defaulted and deleted special methods

6     Standard library enhancements

7     Compiler support

    Ralph Langendam (NCIM-Groep)                 C++ 11   Januari 9th, 2013   23 / 39
New functionality   Variadic templates


Variadic templates




 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.




 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.




 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 )
             {
             }
      };




 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




 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.




 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 .




 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




 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




 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.




 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




 Ralph Langendam (NCIM-Groep)                 C++ 11                                Januari 9th, 2013   29 / 39
New functionality   Defaulted and deleted special methods


Prohibit method calls


      deletecan also be used to prohibit calling methods with specific
      arguments.




 Ralph Langendam (NCIM-Groep)                 C++ 11                                Januari 9th, 2013   29 / 39
New functionality   Defaulted and deleted special methods


Prohibit method calls


      deletecan also be used to prohibit calling methods with specific
      arguments.
      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 enhancements
        Threading facilities
        Smart pointers
        Type traits

7     Compiler support
    Ralph Langendam (NCIM-Groep)                      C++ 11   Januari 9th, 2013   30 / 39
Standard library enhancements   Threading facilities


Threading facilities




 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




 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 variables
      and RAII locks.




 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 variables
      and RAII locks.
      Mutexes can be avoided by using atomic operations and memory
      barriers.




 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 variables
      and RAII locks.
      Mutexes can be avoided by using atomic operations and memory
      barriers.
      Asynchronous thread communication is facilitated by futures and
      promises.


 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 variables
      and RAII locks.
      Mutexes can be avoided by using atomic operations and memory
      barriers.
      Asynchronous thread communication is facilitated by futures and
      promises.
      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




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




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




 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




 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 of
      dynamically allocated space.




 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 of
      dynamically allocated space.
      RAII: Resource Acquisition Is Initialization.




 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 of
      dynamically 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




 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 ) ;




 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);




 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 :: d y n a m i c _ 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




 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 at
      compile time.




 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 at
      compile time.
      struct Base {}; struct Derived : Base {};
      static_assert ( std :: is_base_of < Base , Derived >:: value ,
          " Oops " ) ;




 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 at
      compile 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




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


Compiler support




      C++11 covers about 40 subjects.




 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 CLang
      3.1 are almost feature complete.




 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 CLang
      3.1 are almost feature complete.
      Enabling C++ 11 is automatic for MSVC, but requires an additional
      commandline option -std=c++11 for GCC and CLang.




 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 CLang
      3.1 are almost feature complete.
      Enabling C++ 11 is automatic for MSVC, but requires an additional
      commandline 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

Contenu connexe

Tendances

Tendances (20)

C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Exception handling
Exception handlingException handling
Exception handling
 
Formatted input and output
Formatted input and outputFormatted input and output
Formatted input and output
 
Function in c
Function in cFunction in c
Function in c
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Polymorphism in C++
Polymorphism in C++Polymorphism in C++
Polymorphism in C++
 
Object and class
Object and classObject and class
Object and class
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
OOP C++
OOP C++OOP C++
OOP C++
 
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTREC & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
C & C++ Training Centre in Ambala! BATRA COMPUTER CENTRE
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
This pointer
This pointerThis pointer
This pointer
 
C++
C++C++
C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 

En vedette

En vedette (7)

C++ language
C++ languageC++ language
C++ language
 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
presentation on C++ basics by prince kumar kushwaha
presentation on C++ basics by prince kumar kushwahapresentation on C++ basics by prince kumar kushwaha
presentation on C++ basics by prince kumar kushwaha
 
Intro. to prog. c++
Intro. to prog. c++Intro. to prog. c++
Intro. to prog. c++
 
Intro to C++ - language
Intro to C++ - languageIntro to C++ - language
Intro to C++ - language
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Similaire à 20130110 prs presentation ncim c++ 11

TIM: Large-scale Energy Forecasting in Julia
TIM: Large-scale Energy Forecasting in JuliaTIM: Large-scale Energy Forecasting in Julia
TIM: Large-scale Energy Forecasting in Julia
GapData Institute
 

Similaire à 20130110 prs presentation ncim c++ 11 (20)

ANSI ISO C Professional Programmer S Handbook
ANSI ISO C   Professional Programmer S HandbookANSI ISO C   Professional Programmer S Handbook
ANSI ISO C Professional Programmer S Handbook
 
C++ در STL آشنایی با کتابخانه
C++ در STL آشنایی با کتابخانهC++ در STL آشنایی با کتابخانه
C++ در STL آشنایی با کتابخانه
 
Evolution of programming language
Evolution of programming languageEvolution of programming language
Evolution of programming language
 
02 c++g3 d
02 c++g3 d02 c++g3 d
02 c++g3 d
 
C++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of ClassC++ 11 Style : A Touch of Class
C++ 11 Style : A Touch of Class
 
C++14 - Modern Programming for Demanding Times
C++14 - Modern Programming for Demanding TimesC++14 - Modern Programming for Demanding Times
C++14 - Modern Programming for Demanding Times
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
 
Pydata talk
Pydata talkPydata talk
Pydata talk
 
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
Flink Forward Berlin 2017: Andreas Kunft - Efficiently executing R Dataframes...
 
Ryan Curtin, Principal Research Scientist, Symantec at MLconf ATL 2016
Ryan Curtin, Principal Research Scientist, Symantec at MLconf ATL 2016Ryan Curtin, Principal Research Scientist, Symantec at MLconf ATL 2016
Ryan Curtin, Principal Research Scientist, Symantec at MLconf ATL 2016
 
History of c++
History of c++ History of c++
History of c++
 
Why Python?
Why Python?Why Python?
Why Python?
 
History of c and c++
History of c and c++History of c and c++
History of c and c++
 
C++11
C++11C++11
C++11
 
C++ history session 00 history
C++ history session 00   historyC++ history session 00   history
C++ history session 00 history
 
TIM: Large-scale Energy Forecasting in Julia
TIM: Large-scale Energy Forecasting in JuliaTIM: Large-scale Energy Forecasting in Julia
TIM: Large-scale Energy Forecasting in Julia
 
Evolution of programming languages
Evolution of programming languagesEvolution of programming languages
Evolution of programming languages
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
27 c
27 c27 c
27 c
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 

20130110 prs presentation ncim c++ 11

  • 1. C++ 11 Ralph Langendam NCIM-Groep Januari 9th, 2013 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 1 / 39
  • 2. Overview Anonymous functions 1 Timeline Override and Final History Alias templates Present and Future Enumerations and Unions 2 C++11 Development 5 New functionality Directives Variadic templates Enhancement categories Defaulted and deleted special 3 Run-time performance methods enhancements 6 Standard library enhancements Move semantics Threading facilities Constant expression Smart pointers 4 Usability enhancements Type traits Static assertions 7 Compiler support Type inference 8 Further reading Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 2 / 39
  • 3. Timeline Outline 1 Timeline History Present and Future 2 C++11 Development 3 Run-time performance enhancements 4 Usability enhancements 5 New functionality 6 Standard library enhancements 7 Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 3 / 39
  • 4. Timeline History History Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 5. Timeline History History ≈ 1979 C with classes (CFront): C, classes, inheritance, inline, default argument values, strong type checking. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 6. 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 (//). Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 7. 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++ Programming Language. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 8. 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++ Programming Language. 1989 protected and static members. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 9. 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++ Programming Language. 1989 protected and static members. 1990 Publication of The Annotated C++ Reference Manual, Borland Turbo C++. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 10. 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++ Programming Language. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 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++ Programming Language. 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 12. 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++ Programming Language. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 4 / 39
  • 13. 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++ Programming Language. 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
  • 14. Timeline Present and Future Present and Future Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39
  • 15. Timeline Present and Future Present and Future Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39
  • 16. Timeline Present and Future Present and Future The Boost library project was a large source of inspiration to TR1. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39
  • 17. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39
  • 18. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 5 / 39
  • 19. 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
  • 20. C++11 Development Outline 1 Timeline 2 C++11 Development Directives Enhancement categories 3 Run-time performance enhancements 4 Usability enhancements 5 New functionality 6 Standard library enhancements 7 Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 6 / 39
  • 21. C++11 Development Directives Directives Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 22. C++11 Development Directives Directives Backwards compatibility with C++98 and C. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 23. C++11 Development Directives Directives Backwards compatibility with C++98 and C. Extend primarily through STL and less through C++ core language. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 24. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 25. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 26. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 27. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 7 / 39
  • 28. 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
  • 29. C++11 Development Enhancement categories Enhancement categories Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39
  • 30. C++11 Development Enhancement categories Enhancement categories Run-time performance enhancements Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39
  • 31. C++11 Development Enhancement categories Enhancement categories Run-time performance enhancements Build-time performance enhancements Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39
  • 32. C++11 Development Enhancement categories Enhancement categories Run-time performance enhancements Build-time performance enhancements Usability enhancements Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39
  • 33. C++11 Development Enhancement categories Enhancement categories Run-time performance enhancements Build-time performance enhancements Usability enhancements New functionality Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 8 / 39
  • 34. 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
  • 35. Run-time performance enhancements Outline 1 Timeline 2 C++11 Development 3 Run-time performance enhancements Move semantics Constant expression 4 Usability enhancements 5 New functionality 6 Standard library enhancements 7 Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 9 / 39
  • 36. Run-time performance enhancements Move semantics R-value references Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39
  • 37. 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39
  • 38. 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) ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39
  • 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 10 / 39
  • 40. 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
  • 41. Run-time performance enhancements Move semantics R-value references Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39
  • 42. 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; } Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39
  • 43. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 11 / 39
  • 44. 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
  • 45. Run-time performance enhancements Constant expression Constant expression Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39
  • 46. Run-time performance enhancements Constant expression Constant expression Compile time calculations are ill-formed. int Two () { return 2; } double array [ Two () + 3]; // Ill - formed Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39
  • 47. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 12 / 39
  • 48. 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
  • 49. Run-time performance enhancements Constant expression Constant class expressions Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 13 / 39
  • 50. 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
  • 51. Run-time performance enhancements Constant expression Constant class expressions Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39
  • 52. Run-time performance enhancements Constant expression Constant class expressions constexpr Square ces (3) ; const Square cs (4) ; Square s (5) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39
  • 53. 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 () ]; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 14 / 39
  • 54. 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
  • 55. Usability enhancements Outline 1 Timeline 2 C++11 Development 3 Run-time performance enhancements 4 Usability enhancements Static assertions Type inference Anonymous functions Override and Final Alias templates Enumerations and Unions 5 New functionality 6 Ralph Langendam library enhancements Standard (NCIM-Groep) C++ 11 Januari 9th, 2013 15 / 39
  • 56. Usability enhancements Static assertions Static assertions Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 57. 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 . " ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 58. 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 . " ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 59. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 60. 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 T¨ring complete. u Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 61. 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 T¨ring complete. u All unit tests can be static unit tests! Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 16 / 39
  • 62. Usability enhancements Type inference Automatic type deduction Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39
  • 63. Usability enhancements Type inference Automatic type deduction Inability to overload function based on return type opens up the possibility for type inference. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39
  • 64. Usability enhancements Type inference Automatic type deduction Inability to overload function based on return type opens up the possibility for type inference. Bonus: syntactic sugar std :: set < float >:: const_iterator b ( s . begin () ) ; auto b ( s . begin () ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 17 / 39
  • 65. Usability enhancements Type inference Automatic type deduction Inability to overload function based on return type opens up the possibility 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
  • 66. Usability enhancements Anonymous functions Callbacks and closures Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39
  • 67. Usability enhancements Anonymous functions Callbacks and closures std::function makes callbacks more readable then conventional function pointers. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39
  • 68. Usability enhancements Anonymous functions Callbacks and closures std::function makes callbacks more readable then conventional function pointers. # include < functional > struct S { int TimesTwo ( short s ) const { return 2 * s ; } }; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39
  • 69. Usability enhancements Anonymous functions Callbacks and closures std::function makes callbacks more readable then conventional function 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 18 / 39
  • 70. Usability enhancements Anonymous functions Callbacks and closures std::function makes callbacks more readable then conventional function 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
  • 71. Usability enhancements Anonymous functions Lambda expressions Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 19 / 39
  • 72. Usability enhancements Anonymous functions Lambda expressions Syntax: [capture clause] (parameter list)-> returnvalue {body} Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 19 / 39
  • 73. 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
  • 74. Usability enhancements Override and Final Override and Final Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39
  • 75. Usability enhancements Override and Final Override and Final Use override to avoid accidental creation of new virtual function. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39
  • 76. Usability enhancements Override and Final Override and Final Use override to avoid accidental creation of new virtual function. Use final to prevent further specialization. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 20 / 39
  • 77. 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
  • 78. Usability enhancements Alias templates Alias templates Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39
  • 79. 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 ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39
  • 80. 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 > > {}; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 21 / 39
  • 81. 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
  • 82. Usability enhancements Enumerations and Unions Enumerations and Unions Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39
  • 83. Usability enhancements Enumerations and Unions Enumerations and Unions Strongly typed enumerations Enumeration classes can no longer be implicitly converted to int. enum E ; // Error : unknown underlying type enum class E : unsigned long ; enum class E { A = 3 , B , C }; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39
  • 84. Usability enhancements Enumerations and Unions Enumerations and Unions Strongly typed enumerations Enumeration classes can no longer be implicitly 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 constructible types, but the union constructor needs to be manually defined then. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 22 / 39
  • 85. New functionality Outline 1 Timeline 2 C++11 Development 3 Run-time performance enhancements 4 Usability enhancements 5 New functionality Variadic templates Defaulted and deleted special methods 6 Standard library enhancements 7 Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 23 / 39
  • 86. New functionality Variadic templates Variadic templates Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39
  • 87. New functionality Variadic templates Variadic templates Templated types can have a variadic template parameter list. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39
  • 88. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 24 / 39
  • 89. 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
  • 90. New functionality Variadic templates Variadic templates Specialization: template < typename T > struct Tuple <T > { T _value ; explicit Tuple ( T value ) : _value ( value ) { } }; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 25 / 39
  • 91. 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
  • 92. New functionality Variadic templates std::tuple Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39
  • 93. 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. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39
  • 94. 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 . Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 26 / 39
  • 95. 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
  • 96. New functionality Defaulted and deleted special methods Defaulted special methods Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 27 / 39
  • 97. 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
  • 98. New functionality Defaulted and deleted special methods Default and Delete Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 28 / 39
  • 99. New functionality Defaulted and deleted special methods Default and Delete C++11 allows control over these default methods. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 28 / 39
  • 100. 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
  • 101. New functionality Defaulted and deleted special methods Prohibit method calls Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 29 / 39
  • 102. New functionality Defaulted and deleted special methods Prohibit method calls deletecan also be used to prohibit calling methods with specific arguments. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 29 / 39
  • 103. New functionality Defaulted and deleted special methods Prohibit method calls deletecan also be used to prohibit calling methods with specific arguments. 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
  • 104. Standard library enhancements Outline 1 Timeline 2 C++11 Development 3 Run-time performance enhancements 4 Usability enhancements 5 New functionality 6 Standard library enhancements Threading facilities Smart pointers Type traits 7 Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 30 / 39
  • 105. Standard library enhancements Threading facilities Threading facilities Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 106. 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 107. 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 variables and RAII locks. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 108. 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 variables and RAII locks. Mutexes can be avoided by using atomic operations and memory barriers. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 109. 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 variables and RAII locks. Mutexes can be avoided by using atomic operations and memory barriers. Asynchronous thread communication is facilitated by futures and promises. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 110. 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 variables and RAII locks. Mutexes can be avoided by using atomic operations and memory barriers. Asynchronous thread communication is facilitated by futures and promises. Thread pools are planned for upcoming C++ standards. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 31 / 39
  • 111. Standard library enhancements Smart pointers Memory leaks Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 32 / 39
  • 112. 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 32 / 39
  • 113. 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
  • 114. Standard library enhancements Smart pointers RAII as a solution Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39
  • 115. 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 Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39
  • 116. 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 of dynamically allocated space. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39
  • 117. 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 of dynamically allocated space. RAII: Resource Acquisition Is Initialization. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 33 / 39
  • 118. 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 of dynamically 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
  • 119. Standard library enhancements Smart pointers Smart pointers Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39
  • 120. 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 ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39
  • 121. 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); Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39
  • 122. 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 :: d y n a m i c _ pointer_cast < Base , Derived > ( d ) ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 34 / 39
  • 123. Standard library enhancements Type traits Type traits for TMP Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39
  • 124. Standard library enhancements Type traits Type traits for TMP C++11 elaborates on type computation and transformation at compile time. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39
  • 125. Standard library enhancements Type traits Type traits for TMP C++11 elaborates on type computation and transformation at compile time. struct Base {}; struct Derived : Base {}; static_assert ( std :: is_base_of < Base , Derived >:: value , " Oops " ) ; Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 35 / 39
  • 126. Standard library enhancements Type traits Type traits for TMP C++11 elaborates on type computation and transformation at compile 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
  • 127. 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
  • 128. Compiler support Compiler support Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39
  • 129. Compiler support Compiler support C++11 covers about 40 subjects. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39
  • 130. Compiler support Compiler support C++11 covers about 40 subjects. MSVC 2012 supports about half of them, while GCC 4.8 and CLang 3.1 are almost feature complete. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39
  • 131. Compiler support Compiler support C++11 covers about 40 subjects. MSVC 2012 supports about half of them, while GCC 4.8 and CLang 3.1 are almost feature complete. Enabling C++ 11 is automatic for MSVC, but requires an additional commandline option -std=c++11 for GCC and CLang. Ralph Langendam (NCIM-Groep) C++ 11 Januari 9th, 2013 37 / 39
  • 132. Compiler support Compiler support C++11 covers about 40 subjects. MSVC 2012 supports about half of them, while GCC 4.8 and CLang 3.1 are almost feature complete. Enabling C++ 11 is automatic for MSVC, but requires an additional commandline 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
  • 133. 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
  • 134. 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