• Like
珠三角技术沙龙新语言场 C++11
Upcoming SlideShare
Loading in...5
×

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

  • 1,158 views
Uploaded on

 

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,158
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
67
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. C++11 戏时间 翀
  • 2. C++11 让 们变 轻
  • 3. 纠结
  • 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 Referenceint&& r = foobar(); // r 值赋值 r 变为 值int& r1 = r; // OK r 值 r1 r 值int&& r2 = r; // 错误 值 r2 值 赋值
  • 7. 值 Rvalue Referenceint&& r = foobar(); // r 值赋值 r 变为 值int& r1 = r; // OK r 值 r1 r 值int&& r2 = r; // 错误 值 r2 值 赋值 临时变 时间 长
  • 8. struct A;A CreateA();A a = CreateA();
  • 9. struct A; 认构A CreateA(); 构A a = CreateA(); 构
  • 10. 值 场
  • 11. 值 场struct A;A CreateA();A&& a = CreateA();
  • 12. 值 场struct A; 认构A CreateA(); 构A&& a = CreateA();
  • 13. struct Res { /* */ };struct A { Res* _res; A() : _res(new Res) { } A(A const & rhs) : _res(new Res(rhs)) {} ~A() { delete _res; }};
  • 14. 动构 场struct Res { /* */ };struct A { Res* _res; A() : _res(new Res) { } A(A && another): 传 对 _res(another._res) { another._res = nullptr; } 为临时对 ~A() { delete _res; }};
  • 15. 动 传递调 调
  • 16. 栈对 维护 对auto_ptr 废 unique_ptrunique_ptr 单shared_ptrSTL 实现 动构 构
  • 17. 变长 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);
  • 18. 标 贴 + 载template <class Head, calss Tail> struct typelist;typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>>
  • 19. 标 贴 + 载template <class Head, calss Tail> struct typelist;typelist<T1,typelist<T2,typelist<T3, typelist<T4, NULLType>>>> template<class ...Args> void fun(Args... args);
  • 20. 输 对void PrintObjects() //终{}//递归template <typename Head, typename ...Tails>void PrintObjects(const Head& h, const Tails& ...args){ cout << h; PrintObjects(args...);}// “ ”
  • 21. Templates Aliases 标 们 义#define MyVector(T) std::vector<T, MyAlloc<T>>继 构
  • 22. 标简单template <typname T>using MyVector = std::vector<T>template <typname T, typename A>using MyVector =std::vector<T, typename MyAlloc<A>::Concrete>
  • 23. auto 键 现单变auto i = 1; // i为int; 变auto i = 1, j = 1.1, k = 0.5; // i 为auto f(int i, int j) -> int; //为
  • 24. auto decltypedecltype(1); // intdecltype(1 + 1.0); // doubleauto i = 1; // 态typdef decltype(1) __type1;__type1 i = 1; 时 达 结auto add(U u, V v) -> decltype(u + v); u v 现 u vdecltype 简 达 结 导
  • 25. Initializer listsC语 组int arr[] = {1, 2, 3};结构struct A { int i; long j; };A a = {1, 2L};
  • 26. C语组 语 级 义结构 员 为 员
  • 27. 标 统 语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};
  • 28. 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
  • 29. 态/static CnHashMap<std::string, int> MyMap;struct MapInitializer{ MapInitializer() { MyMap.insert(make_pair("one", 1)); MyMap.insert(make_pair("two", 2)); ...... }};static MapInitializor initializer;
  • 30. 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; };
  • 31. lambdaint 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.
  • 32. 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
  • 33. 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();
  • 34. 谢 谢xorcererzc@gmail.com