SlideShare une entreprise Scribd logo
1  sur  35
C++11       戏时间
        翀
C++11   让   们变 轻
纠结
值        Rvalue Reference         动构     Move Constructor

变长             Variadic templates

          Templates Aliases

 动        导    decltype    auto   键

               Initializer lists

 态        Static Assertion        简

      构       Delegating Constructors   简

显         认      删        Defaulted and deleted functions    简

Raw           Unicode      Raw strings and Unicode strings       简

lambda    达
值       Rvalue

                载       运                            值
         为临时对                        变                    值

int foobar();
int j;
j = foobar(); // foobar()            值为       值
int* p = &foobar(); // 错误                             值
int& r = foobar(); // 错误                            值
j = 42; // 42   值




                        http://thbecker.net/articles/rvalue_references/section_01.html
值            Rvalue Reference

int&& r = foobar(); // r        值赋值 r        变为   值
int& r1 = r; // OK   r     值        r1       r    值
int&& r2 = r; // 错误      值     r2        值   赋值
值            Rvalue Reference

int&& r = foobar(); // r        值赋值 r            变为   值
int& r1 = r; // OK   r     值        r1           r    值
int&& r2 = r; // 错误      值     r2            值   赋值




   临时变                     时间            长
struct A;

A CreateA();

A a = CreateA();
struct A;          认构


A CreateA();
                    构
A a = CreateA();

                   构
值   场
值     场

struct A;

A CreateA();

A&& a = CreateA();
值     场

struct A;
                     认构

A CreateA();

                         构
A&& a = CreateA();
struct Res { /*         */ };


struct A {
    Res* _res;
    A() : _res(new Res) { }

     A(A const & rhs) :
       _res(new Res(rhs)) {}

     ~A() { delete _res; }
};
动构               场
struct Res { /*         */ };


struct A {
    Res* _res;
    A() : _res(new Res) { }

     A(A && another):                  传   对
     _res(another._res) {
         another._res = nullptr;
     }
                                   为临时对
     ~A() { delete _res; }
};
动



            传递




调   调
栈对       维护           对
auto_ptr     废      unique_ptr

unique_ptr      单

shared_ptr

STL

      实现   动构        构
变长
                  Variadic templates

                                              狱
template <typename R, typename P1>
R call(const std::string& name, P1 arg1, R default_value);

template <typename R, typename P1, typename P2>
R call(const std::string& name, P1 arg1, P2 arg2, R default_value);

template <typename R, typename P1, typename P2, typename P3>
R call(const std::string& name, P1 arg1, P2 arg2, P3 arg3, R default_value);

template <typename R, typename P1, typename P2, typename P3, typename P4>
R call(const std::string& name, P1 arg1, P2 arg2, P3 arg3, P4 arg4, R default_value);
标
        贴 +        载
template <class Head, calss Tail> struct typelist;
typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>>
标
        贴 +        载
template <class Head, calss Tail> struct typelist;
typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>>




 template<class ...Args> void fun(Args... args);
输                   对
void PrintObjects() //终
{
}

//递归
template <typename Head, typename ...Tails>
void PrintObjects(const Head& h, const Tails& ...args)
{
    cout << h;
    PrintObjects(args...);
}
//    “          ”
Templates Aliases


    标            们

    义
#define MyVector(T) std::vector<T, MyAlloc<T>>


继                  构
标


简单
template <typname T>
using MyVector = std::vector<T>




template <typname T, typename A>
using MyVector =
std::vector<T, typename MyAlloc<A>::Concrete>
auto            键
  现

单变
auto i = 1; // i为int;


    变
auto i = 1, j = 1.1, k = 0.5; //   i   为




auto f(int i, int j) -> int; //为
auto                                   decltype
decltype(1); //          int
decltype(1 + 1.0); //          double


auto i = 1; //           态
typdef decltype(1) __type1;
__type1 i = 1;

                     时                      达   结
auto add(U u, V v) -> decltype(u + v);
    u    v        现                                 u   v


decltype         简             达   结    导
Initializer lists

C语

 组
int arr[] = {1, 2, 3};

结构
struct A { int i; long j; };
A a = {1, 2L};
C语

组    语   级       义



结构       员   为       员
标              统          语
std::initializer_list<T>对       过{}语         C++


initializer_list<int> il = {1, 2, 3, 4, 5, 6};

 构           initializer_list<T>为
vector<int> v1({1, 2, 3}); //
vector<int> v2 = {1, 2, 3};

     initializer_list<T>构              {}语   ()语
MyClass my(1); //
MyClass my{1};
vector<int> v1(3);
//         vector 调      构     vector(int size);
vector<int> v2{3};
//           为3 vector     调    构
vector(initializer_list<int> list);

map<string, int> m = {{1, 2}, {3, 4}};
//调    构     map(initializer_list<pair<string, int>>)
      {}语           pair
态/
static CnHashMap<std::string, int> MyMap;

struct MapInitializer
{
	 MapInitializer()
	 {
	 	 MyMap.insert(make_pair("one", 1));
	 	 MyMap.insert(make_pair("two", 2));
       ......
	 }
};

static MapInitializor initializer;
Lambda                   达
int local_i = 10;
std::function<void(int, int)> f =
  [=local_i] //                  变
 (int i, int j) -> int //                     值
 {
      //       值         则   过        达   结       导
      // decltype(local_i + i + j);
      return local_i + i + j;
 };
lambda
int i = 10;
auto f = [=] { cout << i << endl; };
cout << sizeof(f) << endl; //    4   仅      int   间


struct A {
     int j;
     void operator() () {
         cout << j << endl;
     }
};
A a = {i};
cout << sizeof(A) << endl; //        样为4.
int i = 10;
auto f = [=] { cout << i << endl; };
cout << sizeof(f) << endl;
//       4   仅        int   间


struct F {
     void operator() (int k) {
             cout << k << endl;
     }
};
auto boost_f = boost::bind(&F::operator (), i);
cout << sizeof(boost_f) << endl;
//       为24
struct B {
    int _i;
    long _l1;
    long _l2;
    long _l3;
    void call()   {
        auto g1   = [=] { cout << _i << endl; };
        auto g2   = [=] { cout << _i << _l1 << _l2 << _l3 << endl; };
        auto g3   = [&] { cout << _i << _l1 << _l2 << _l3 << endl; };
        cout <<   sizeof(g1) << endl; //     8
         cout << sizeof(g2) << endl; //       8
         cout << sizeof(g3) << endl; //       8
         //        lambda   达    仅仅       B   this   针
     }
};

B a = {0, 1L, 2L, 3L, 4L};
a.call();
谢 谢
xorcererzc@gmail.com

Contenu connexe

Tendances

Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections corehard_by
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеPlatonov Sergey
 
CTF問題解説 OSのタスク切換え
CTF問題解説 OSのタスク切換えCTF問題解説 OSのタスク切換え
CTF問題解説 OSのタスク切換えmono0x
 
Sujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionSujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionborhen boukthir
 
Hyrje openmp
Hyrje openmpHyrje openmp
Hyrje openmpL Dr
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docxmiftah88
 
Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?phpGeekMeet_ro
 
Most Common JavaScript Mistakes
Most Common JavaScript MistakesMost Common JavaScript Mistakes
Most Common JavaScript MistakesYoann Gotthilf
 

Tendances (20)

Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
C++14 reflections
C++14 reflections C++14 reflections
C++14 reflections
 
Gjuha paskal
Gjuha paskal Gjuha paskal
Gjuha paskal
 
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговлеТененёв Анатолий, Boost.Asio в алгоритмической торговле
Тененёв Анатолий, Boost.Asio в алгоритмической торговле
 
JQuery
JQueryJQuery
JQuery
 
CTF問題解説 OSのタスク切換え
CTF問題解説 OSのタスク切換えCTF問題解説 OSのタスク切換え
CTF問題解説 OSのタスク切換え
 
Sujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correctionSujet bac info 2012 g1, g2 et g3 avec correction
Sujet bac info 2012 g1, g2 et g3 avec correction
 
Librerias de c++
Librerias de c++Librerias de c++
Librerias de c++
 
week-24x
week-24xweek-24x
week-24x
 
latihan SAP
latihan SAPlatihan SAP
latihan SAP
 
Hyrje openmp
Hyrje openmpHyrje openmp
Hyrje openmp
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docx
 
Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?Adi Fatol - Ce e nou in PHP 5.3?
Adi Fatol - Ce e nou in PHP 5.3?
 
Most Common JavaScript Mistakes
Most Common JavaScript MistakesMost Common JavaScript Mistakes
Most Common JavaScript Mistakes
 
es6.concurrency()
es6.concurrency()es6.concurrency()
es6.concurrency()
 
Programming in C
Programming in CProgramming in C
Programming in C
 
F jxlo06xw
F jxlo06xwF jxlo06xw
F jxlo06xw
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Ejercicios c#
Ejercicios c#Ejercicios c#
Ejercicios c#
 
Php Meets Messagepack
Php Meets MessagepackPhp Meets Messagepack
Php Meets Messagepack
 

En vedette

LLVM, clang & c++
LLVM, clang & c++LLVM, clang & c++
LLVM, clang & c++cppfrug
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingcppfrug
 
How to Battle Bad Reviews
How to Battle Bad ReviewsHow to Battle Bad Reviews
How to Battle Bad ReviewsGlassdoor
 
The Near Future of CSS
The Near Future of CSSThe Near Future of CSS
The Near Future of CSSRachel Andrew
 
Classroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and AdolescentsClassroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and AdolescentsShelly Sanchez Terrell
 

En vedette (6)

LLVM, clang & c++
LLVM, clang & c++LLVM, clang & c++
LLVM, clang & c++
 
C++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogrammingC++ How I learned to stop worrying and love metaprogramming
C++ How I learned to stop worrying and love metaprogramming
 
How to Battle Bad Reviews
How to Battle Bad ReviewsHow to Battle Bad Reviews
How to Battle Bad Reviews
 
The Near Future of CSS
The Near Future of CSSThe Near Future of CSS
The Near Future of CSS
 
Classroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and AdolescentsClassroom Management Tips for Kids and Adolescents
Classroom Management Tips for Kids and Adolescents
 
Back-to-School Survey 2016
Back-to-School Survey 2016Back-to-School Survey 2016
Back-to-School Survey 2016
 

珠三角技术沙龙新语言场 C++11

  • 1. C++11 戏时间 翀
  • 2. C++11 让 们变 轻
  • 4. Rvalue Reference 动构 Move Constructor 变长 Variadic templates Templates Aliases 动 导 decltype auto 键 Initializer lists 态 Static Assertion 简 构 Delegating Constructors 简 显 认 删 Defaulted and deleted functions 简 Raw Unicode Raw strings and Unicode strings 简 lambda 达
  • 5. Rvalue 载 运 值 为临时对 变 值 int foobar(); int j; j = foobar(); // foobar() 值为 值 int* p = &foobar(); // 错误 值 int& r = foobar(); // 错误 值 j = 42; // 42 值 http://thbecker.net/articles/rvalue_references/section_01.html
  • 6. Rvalue Reference int&& r = foobar(); // r 值赋值 r 变为 值 int& r1 = r; // OK r 值 r1 r 值 int&& r2 = r; // 错误 值 r2 值 赋值
  • 7. Rvalue Reference int&& r = foobar(); // r 值赋值 r 变为 值 int& r1 = r; // OK r 值 r1 r 值 int&& r2 = r; // 错误 值 r2 值 赋值 临时变 时间 长
  • 8.
  • 9. struct A; A CreateA(); A a = CreateA();
  • 10. struct A; 认构 A CreateA(); 构 A a = CreateA(); 构
  • 11.
  • 12. 场 struct A; A CreateA(); A&& a = CreateA();
  • 13. 场 struct A; 认构 A CreateA(); 构 A&& a = CreateA();
  • 14. struct Res { /* */ }; struct A { Res* _res; A() : _res(new Res) { } A(A const & rhs) : _res(new Res(rhs)) {} ~A() { delete _res; } };
  • 15. 动构 场 struct Res { /* */ }; struct A { Res* _res; A() : _res(new Res) { } A(A && another): 传 对 _res(another._res) { another._res = nullptr; } 为临时对 ~A() { delete _res; } };
  • 16. 传递 调 调
  • 17. 栈对 维护 对 auto_ptr 废 unique_ptr unique_ptr 单 shared_ptr STL 实现 动构 构
  • 18. 变长 Variadic templates 狱 template <typename R, typename P1> R call(const std::string& name, P1 arg1, R default_value); template <typename R, typename P1, typename P2> R call(const std::string& name, P1 arg1, P2 arg2, R default_value); template <typename R, typename P1, typename P2, typename P3> R call(const std::string& name, P1 arg1, P2 arg2, P3 arg3, R default_value); template <typename R, typename P1, typename P2, typename P3, typename P4> R call(const std::string& name, P1 arg1, P2 arg2, P3 arg3, P4 arg4, R default_value);
  • 19. 贴 + 载 template <class Head, calss Tail> struct typelist; typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>>
  • 20. 贴 + 载 template <class Head, calss Tail> struct typelist; typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>> template<class ...Args> void fun(Args... args);
  • 21. 对 void PrintObjects() //终 { } //递归 template <typename Head, typename ...Tails> void PrintObjects(const Head& h, const Tails& ...args) { cout << h; PrintObjects(args...); } // “ ”
  • 22. Templates Aliases 标 们 义 #define MyVector(T) std::vector<T, MyAlloc<T>> 继 构
  • 23. 标 简单 template <typname T> using MyVector = std::vector<T> template <typname T, typename A> using MyVector = std::vector<T, typename MyAlloc<A>::Concrete>
  • 24. auto 键 现 单变 auto i = 1; // i为int; 变 auto i = 1, j = 1.1, k = 0.5; // i 为 auto f(int i, int j) -> int; //为
  • 25. auto decltype decltype(1); // int decltype(1 + 1.0); // double auto i = 1; // 态 typdef decltype(1) __type1; __type1 i = 1; 时 达 结 auto add(U u, V v) -> decltype(u + v); u v 现 u v decltype 简 达 结 导
  • 26. Initializer lists C语 组 int arr[] = {1, 2, 3}; 结构 struct A { int i; long j; }; A a = {1, 2L};
  • 27. C语 组 语 级 义 结构 员 为 员
  • 28. 统 语 std::initializer_list<T>对 过{}语 C++ initializer_list<int> il = {1, 2, 3, 4, 5, 6}; 构 initializer_list<T>为 vector<int> v1({1, 2, 3}); // vector<int> v2 = {1, 2, 3}; initializer_list<T>构 {}语 ()语 MyClass my(1); // MyClass my{1};
  • 29. vector<int> v1(3); // vector 调 构 vector(int size); vector<int> v2{3}; // 为3 vector 调 构 vector(initializer_list<int> list); map<string, int> m = {{1, 2}, {3, 4}}; //调 构 map(initializer_list<pair<string, int>>) {}语 pair
  • 30. 态/ static CnHashMap<std::string, int> MyMap; struct MapInitializer { MapInitializer() { MyMap.insert(make_pair("one", 1)); MyMap.insert(make_pair("two", 2)); ...... } }; static MapInitializor initializer;
  • 31. Lambda 达 int local_i = 10; std::function<void(int, int)> f = [=local_i] // 变 (int i, int j) -> int // 值 { // 值 则 过 达 结 导 // decltype(local_i + i + j); return local_i + i + j; };
  • 32. lambda int i = 10; auto f = [=] { cout << i << endl; }; cout << sizeof(f) << endl; // 4 仅 int 间 struct A { int j; void operator() () { cout << j << endl; } }; A a = {i}; cout << sizeof(A) << endl; // 样为4.
  • 33. int i = 10; auto f = [=] { cout << i << endl; }; cout << sizeof(f) << endl; // 4 仅 int 间 struct F { void operator() (int k) { cout << k << endl; } }; auto boost_f = boost::bind(&F::operator (), i); cout << sizeof(boost_f) << endl; // 为24
  • 34. struct B { int _i; long _l1; long _l2; long _l3; void call() { auto g1 = [=] { cout << _i << endl; }; auto g2 = [=] { cout << _i << _l1 << _l2 << _l3 << endl; }; auto g3 = [&] { cout << _i << _l1 << _l2 << _l3 << endl; }; cout << sizeof(g1) << endl; // 8 cout << sizeof(g2) << endl; // 8 cout << sizeof(g3) << endl; // 8 // lambda 达 仅仅 B this 针 } }; B a = {0, 1L, 2L, 3L, 4L}; a.call();

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n