SlideShare a Scribd company logo
1 of 58
New in C++11 
Trần Duy Quang – May 2014
Agenda 
1. Automatic variables 
2. Decltype 
3. Rvalue reference 
4. Lambda function 
5. Variadic template 
6. Concurrency library 
2
Compiler support 
3 
VS 2010 
Automatic 
variables 
Decltype 
Rvalue 
reference 
Lambda 
function 
VS 2012 
Concurrency 
library 
Memory 
model 
VS 2013 
Variadic 
template 
Custom 
literal 
Delegating 
constructor 
VS 13: Some C++14 features!
auto Variables 
 Implicit variable declaration 
 Keyword: auto 
Note: cbegin to iterate constant, no change in content 4
Careful: auto default 
 By-value for references 
 Use auto& or decltype for desired result! 
5 
int f, not int& f!
More mindblow 
 int&: auto is int (only, no &) 
 But int*: auto is int* ! 
6
std::bind 
 Placeholder objects _1, _2, .., _N 
 C# alike 
7 
1. May cause runtime error, but when? 
2. What about _10 rather than _2?
Range-based for loop 
8 
Better? auto&!
decltype 
 Query the type of an expression 
 Primary use in generic programming1 
Why do we have to use auto & decltype here? 
1Algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters 9
auto without decltype 
 Compile error 
10
Why auto & decltype? 
11
Parenthesized variable 
 Always a reference! 
12
decltype vs auto 
 decltype of a variable 
 returns the type of that variable, including top-level 
const and references 
 decltype depends on its form of variable 
 Parenthesis always yield a reference 
13 
C++ Primer 2014:
What do we have? 
14 
Type of x 
Type of y 
Why? 
decltype(x) y auto y = x 
const int const int int Strips top-level cv-qualifiers 
int[100] int[100] int* Performs array to pointer conversion 
int f (int) int f (int) int (*) (int) Performs function to function pointer 
conversion 
int& int& int Auto remove references 
int&& int&& int 
Don’t forget the reference with parenthesis from decltype! decltype( (x) )
auto vs decltype conclustion 
 auto 
 Everyday use 
 Assign the value of some expression to a new variable 
 decltype 
 Template & generic programming (library code) 
 a new variable with precisely the same type 
15
More mindblow (again) 
 decltype(auto) f = expression; // C++14 
 C++17 will go further? No ideas! 
16
Lambda function 
17
Lambda function 
 []: capture list 
 [] () -> return type 
18
Lambda capture 
 Capture by value 
 42 
 Capture by reference 
 43 
 43 
 (Can be modify 
inside, but not affect 
outside) 
19
Types of capture 
 [=]: automatic capture all by value 
 [&]: automatic capture all by reference 
 [this]: capture this pointer by value 
 [a, &b]: capture a by value and b by reference 
20
Recursive lambda 
21
Stateless lambda 
 Convertible to function pointer! 
 Used with C-style API (e.g. Win32) 
22
High-order function 
 Takes one or more functions as an input 
 Outputs a function 
23 
Sắp xếp tăng dần
Generic lambda 
 Take a deep breath! (C++14 only) 
 We can write 
 auto auto (auto auto) { auto; }; 
 Compiler infers the rest from the context 
 Actually, this is NOT a type deduction 
 This is a template! 
24
Let’s see one generic lambda 
25 Did I tell you about functor?
Lambda physiology 
 For each lambda compiler generate a class 
 Specifying lambda instantiate an object 
 Invoking lambda call the object’s operator() 
26
Rvalue reference 
27
Rvalue definition 
 lvalue: can appear on the left and right 
 rvalue: can only appear on the right 
28
Move semantics 
 What do we need from the last line? 
 Destruct the resource held by hello first 
 Clone resource from temp returned 
 Destruct temp & release its resources 
29 
 More efficient? 
 Swap resources pointers 
 Let temp destructor destruct hello’s original resources
Obvious solution 
 Overload operator= ! (Copy assignment) 
 Right hand side should be passed by reference 
 rvalue references! 
 MyString&&: rvalue reference to MyString& 
30
Function overload resolution 
 “Am I being called on an lvalue or rvalue?” 
31
Achieving move semantics 
 Should occur only for copy constructor and 
assignment operator! 
32
Forcing move semantics 
 std::move help 
 Type with no move semantics implementation? 
 Swap like old times! 
33
Funny jokes 
34
Smart pointers 
35
Three types of smart pointer 
 unique_ptr<T> - Single ownership 
 Deleted when pointer dies 
 shared_ptr<T> - Shared ownership 
 Reference pointer counting 
 weak_ptr<T> - Cycle breaking 
 auto_ptr deprecated! 
36
Before we go on 
 Resource Acquisition Is Initialization (RAII) 
37
 RAII in C++ has been around for over a decade 
 C++11 encourages its use as default 
 Smart pointers help building RAII around legacy 
interfaces 
 Move semantics makes passing resources around 
cheap 
38
unique_ptr 
39
shared_ptr 
 Last pointer dies, object is deleted 
40
weak_ptr 
 Does not affect reference count 
 Break cycles between shared_ptrs 
41
Some other things 
42
Threads & Async 
43
Variadic template 
44
Rules of expansion 
45
Initializer list 
46
using 
47
Raw string literals 
48
Other examples of raw string literals 
49
Tuple 
50
Default & delete 
51
The last coffee drop 
52
C++ Accelerated Massive 
Parallelism 
53
Simple example 
54 
Normal version AMP version
Basic elements of AMP 
55 
Code run on 
each thread 
Thread id 
running the 
lambda 
Check if code can run 
with AMP / Direct3D
Reference 1 
 Slide C++11 
 Slide What’s new in C++11 
 Slide The future of C++ 
 Slide The style of C++11 
 Slide C++11 
 Slide C++11 idioms 
 Slide What’s new in Visual C++11 
 Slide C++11 – Feel the new language 
 Slide C++11 Talk 
56
Reference 2 
 Slide C++11 – A change in style 
 Slide New C++ Standard – C++11 
 Slide Advanced C++ runtime improvement 
techniques 
 Slide Hot C+11 Rvalue references and Move 
semantics 
 Slide C++ usage experience 
57
Reference 3 
 Ebook C++11 for programmers 2014 
 Ebook C++ Primer 5th Edition 2013 
 Ebook C Primer Plus 2014 
 Ebook Effective C++ 3rd Ed 55 Specific Ways to 
Improve Your Programs and Designs 2005 
 Ebook Effective C++ & More effective C++ 
58

More Related Content

What's hot

C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
mspline
 

What's hot (20)

Modern C++
Modern C++Modern C++
Modern C++
 
C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Language
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
C++11
C++11C++11
C++11
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
C++ 11
C++ 11C++ 11
C++ 11
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
C++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect ForwardingC++11: Rvalue References, Move Semantics, Perfect Forwarding
C++11: Rvalue References, Move Semantics, Perfect Forwarding
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
C++ theory
C++ theoryC++ theory
C++ theory
 
C++ Training
C++ TrainingC++ Training
C++ Training
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)Fun with Lambdas: C++14 Style (part 2)
Fun with Lambdas: C++14 Style (part 2)
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 
C++20 the small things - Timur Doumler
C++20 the small things - Timur DoumlerC++20 the small things - Timur Doumler
C++20 the small things - Timur Doumler
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 

Viewers also liked

Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
Kevin III
 

Viewers also liked (20)

Changes in c++0x
Changes in c++0xChanges in c++0x
Changes in c++0x
 
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
[OLD VERSION, SEE DESCRIPTION FOR NEWER VERSION LINK] Hot C++: Rvalue Referen...
 
C++ Builder 程式撰寫基礎 / C++ Builder Basic
C++ Builder 程式撰寫基礎 / C++ Builder Basic C++ Builder 程式撰寫基礎 / C++ Builder Basic
C++ Builder 程式撰寫基礎 / C++ Builder Basic
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP Protocol
 
Bjarne essencegn13
Bjarne essencegn13Bjarne essencegn13
Bjarne essencegn13
 
"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin Upadhyay"Http protocol and other stuff" by Bipin Upadhyay
"Http protocol and other stuff" by Bipin Upadhyay
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol Basic
 
C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11
 
Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6Networking - TCP/IP stack introduction and IPv6
Networking - TCP/IP stack introduction and IPv6
 
Database connectivity to sql server asp.net
Database connectivity to sql server asp.netDatabase connectivity to sql server asp.net
Database connectivity to sql server asp.net
 
C++11 Idioms @ Silicon Valley Code Camp 2012
C++11 Idioms @ Silicon Valley Code Camp 2012 C++11 Idioms @ Silicon Valley Code Camp 2012
C++11 Idioms @ Silicon Valley Code Camp 2012
 
C++14 Overview
C++14 OverviewC++14 Overview
C++14 Overview
 
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# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesC# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slides
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointers
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examples
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and Beyond
 
HTTPS: What, Why and How (SmashingConf Freiburg, Sep 2015)
HTTPS: What, Why and How (SmashingConf Freiburg, Sep 2015)HTTPS: What, Why and How (SmashingConf Freiburg, Sep 2015)
HTTPS: What, Why and How (SmashingConf Freiburg, Sep 2015)
 

Similar to C++11

C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
techfreak
 

Similar to C++11 (20)

Intro to c++
Intro to c++Intro to c++
Intro to c++
 
The Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 MigrationThe Goal and The Journey - Turning back on one year of C++14 Migration
The Goal and The Journey - Turning back on one year of C++14 Migration
 
Whats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ DevelopersWhats New in Visual Studio 2012 for C++ Developers
Whats New in Visual Studio 2012 for C++ Developers
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The Lambda
 
Gude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic ServerGude for C++11 in Apache Traffic Server
Gude for C++11 in Apache Traffic Server
 
C_plus_plus
C_plus_plusC_plus_plus
C_plus_plus
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreter
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answers
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)2.Getting Started with C#.Net-(C#)
2.Getting Started with C#.Net-(C#)
 
Rcpp
RcppRcpp
Rcpp
 
Technical trainning.pptx
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptx
 
C material
C materialC material
C material
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptx
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-
 
LLVM
LLVMLLVM
LLVM
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginners
 
C#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New FeaturesC#3.0 & Vb 9.0 New Features
C#3.0 & Vb 9.0 New Features
 
C programming
C programmingC programming
C programming
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 

C++11

  • 1. New in C++11 Trần Duy Quang – May 2014
  • 2. Agenda 1. Automatic variables 2. Decltype 3. Rvalue reference 4. Lambda function 5. Variadic template 6. Concurrency library 2
  • 3. Compiler support 3 VS 2010 Automatic variables Decltype Rvalue reference Lambda function VS 2012 Concurrency library Memory model VS 2013 Variadic template Custom literal Delegating constructor VS 13: Some C++14 features!
  • 4. auto Variables  Implicit variable declaration  Keyword: auto Note: cbegin to iterate constant, no change in content 4
  • 5. Careful: auto default  By-value for references  Use auto& or decltype for desired result! 5 int f, not int& f!
  • 6. More mindblow  int&: auto is int (only, no &)  But int*: auto is int* ! 6
  • 7. std::bind  Placeholder objects _1, _2, .., _N  C# alike 7 1. May cause runtime error, but when? 2. What about _10 rather than _2?
  • 8. Range-based for loop 8 Better? auto&!
  • 9. decltype  Query the type of an expression  Primary use in generic programming1 Why do we have to use auto & decltype here? 1Algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters 9
  • 10. auto without decltype  Compile error 10
  • 11. Why auto & decltype? 11
  • 12. Parenthesized variable  Always a reference! 12
  • 13. decltype vs auto  decltype of a variable  returns the type of that variable, including top-level const and references  decltype depends on its form of variable  Parenthesis always yield a reference 13 C++ Primer 2014:
  • 14. What do we have? 14 Type of x Type of y Why? decltype(x) y auto y = x const int const int int Strips top-level cv-qualifiers int[100] int[100] int* Performs array to pointer conversion int f (int) int f (int) int (*) (int) Performs function to function pointer conversion int& int& int Auto remove references int&& int&& int Don’t forget the reference with parenthesis from decltype! decltype( (x) )
  • 15. auto vs decltype conclustion  auto  Everyday use  Assign the value of some expression to a new variable  decltype  Template & generic programming (library code)  a new variable with precisely the same type 15
  • 16. More mindblow (again)  decltype(auto) f = expression; // C++14  C++17 will go further? No ideas! 16
  • 18. Lambda function  []: capture list  [] () -> return type 18
  • 19. Lambda capture  Capture by value  42  Capture by reference  43  43  (Can be modify inside, but not affect outside) 19
  • 20. Types of capture  [=]: automatic capture all by value  [&]: automatic capture all by reference  [this]: capture this pointer by value  [a, &b]: capture a by value and b by reference 20
  • 22. Stateless lambda  Convertible to function pointer!  Used with C-style API (e.g. Win32) 22
  • 23. High-order function  Takes one or more functions as an input  Outputs a function 23 Sắp xếp tăng dần
  • 24. Generic lambda  Take a deep breath! (C++14 only)  We can write  auto auto (auto auto) { auto; };  Compiler infers the rest from the context  Actually, this is NOT a type deduction  This is a template! 24
  • 25. Let’s see one generic lambda 25 Did I tell you about functor?
  • 26. Lambda physiology  For each lambda compiler generate a class  Specifying lambda instantiate an object  Invoking lambda call the object’s operator() 26
  • 28. Rvalue definition  lvalue: can appear on the left and right  rvalue: can only appear on the right 28
  • 29. Move semantics  What do we need from the last line?  Destruct the resource held by hello first  Clone resource from temp returned  Destruct temp & release its resources 29  More efficient?  Swap resources pointers  Let temp destructor destruct hello’s original resources
  • 30. Obvious solution  Overload operator= ! (Copy assignment)  Right hand side should be passed by reference  rvalue references!  MyString&&: rvalue reference to MyString& 30
  • 31. Function overload resolution  “Am I being called on an lvalue or rvalue?” 31
  • 32. Achieving move semantics  Should occur only for copy constructor and assignment operator! 32
  • 33. Forcing move semantics  std::move help  Type with no move semantics implementation?  Swap like old times! 33
  • 36. Three types of smart pointer  unique_ptr<T> - Single ownership  Deleted when pointer dies  shared_ptr<T> - Shared ownership  Reference pointer counting  weak_ptr<T> - Cycle breaking  auto_ptr deprecated! 36
  • 37. Before we go on  Resource Acquisition Is Initialization (RAII) 37
  • 38.  RAII in C++ has been around for over a decade  C++11 encourages its use as default  Smart pointers help building RAII around legacy interfaces  Move semantics makes passing resources around cheap 38
  • 40. shared_ptr  Last pointer dies, object is deleted 40
  • 41. weak_ptr  Does not affect reference count  Break cycles between shared_ptrs 41
  • 49. Other examples of raw string literals 49
  • 52. The last coffee drop 52
  • 53. C++ Accelerated Massive Parallelism 53
  • 54. Simple example 54 Normal version AMP version
  • 55. Basic elements of AMP 55 Code run on each thread Thread id running the lambda Check if code can run with AMP / Direct3D
  • 56. Reference 1  Slide C++11  Slide What’s new in C++11  Slide The future of C++  Slide The style of C++11  Slide C++11  Slide C++11 idioms  Slide What’s new in Visual C++11  Slide C++11 – Feel the new language  Slide C++11 Talk 56
  • 57. Reference 2  Slide C++11 – A change in style  Slide New C++ Standard – C++11  Slide Advanced C++ runtime improvement techniques  Slide Hot C+11 Rvalue references and Move semantics  Slide C++ usage experience 57
  • 58. Reference 3  Ebook C++11 for programmers 2014  Ebook C++ Primer 5th Edition 2013  Ebook C Primer Plus 2014  Ebook Effective C++ 3rd Ed 55 Specific Ways to Improve Your Programs and Designs 2005  Ebook Effective C++ & More effective C++ 58

Editor's Notes

  1. Chú ý: cbegin là duyệt container hằng số (constant begin), duyệt không cho phép thay đổi cấu trúc / dữ liệu
  2. Inside the for, you need to specify the alias auto& in order to avoid to create a copy of the elements inside the vector within the thread variable. In this way every operations done on the thread var is done on the element inside the threads vector. Moreover, in a range-based for, you always want to use a reference & for performance reasons.
  3. In the C++ programming language, decltype is a keyword used to query the type of an expression. It was introduced in the current version of the C++ standard, C++11. Its primary intended use is in generic programming, where it is often difficult, or even impossible, to express types that depend on template parameters. Algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. This approach, pioneered by ML in 1973,[citation needed] permits writing common functions or types that differ only in the set of types on which they operate when used, thus reducing duplication
  4. Có thể tự viết các string literal của riêng mình