SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

C++11: Feel the New Language
C++11: Feel the New LanguageC++11: Feel the New Language
C++11: Feel the New Languagemspline
 
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)Sumant Tambe
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
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 ForwardingFrancesco Casalegno
 
[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...Francesco Casalegno
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 
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)Sumant Tambe
 
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 Doumlercorehard_by
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++Ilio Catallo
 

La actualidad más candente (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++
 

Destacado

[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...Andrey Upadyshev
 
C++ Builder 程式撰寫基礎 / C++ Builder Basic
C++ Builder 程式撰寫基礎 / C++ Builder Basic C++ Builder 程式撰寫基礎 / C++ Builder Basic
C++ Builder 程式撰寫基礎 / C++ Builder Basic YKLee3434
 
Web I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolWeb I - 05 - HTTP Protocol
Web I - 05 - HTTP ProtocolRandy Connolly
 
"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 UpadhyayBipin Upadhyay
 
HTTP Protocol Basic
HTTP Protocol BasicHTTP Protocol Basic
HTTP Protocol BasicChuong Mai
 
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)/Sławomir Zborowski
 
Elements of C++11
Elements of C++11Elements of C++11
Elements of C++11Uilian Ries
 
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 IPv6Rodolfo Kohn
 
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.netHemant Sankhla
 
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 Sumant Tambe
 
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 ClassYogendra Rampuria
 
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-slidesSami Mut
 
C++11 smart pointers
C++11 smart pointersC++11 smart pointers
C++11 smart pointerschchwy Chang
 
Data structures / C++ Program examples
Data structures / C++ Program examplesData structures / C++ Program examples
Data structures / C++ Program examplesKevin III
 
Cpp17 and Beyond
Cpp17 and BeyondCpp17 and Beyond
Cpp17 and BeyondComicSansMS
 
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)Guy Podjarny
 

Destacado (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 a C++11

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 MigrationJoel Falcou
 
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++ DevelopersRainer Stropek
 
Can't Dance The Lambda
Can't Dance The LambdaCan't Dance The Lambda
Can't Dance The LambdaTogakangaroo
 
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 ServerApache Traffic Server
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreterRoberto Nogueira
 
C++ question and answers
C++ question and answersC++ question and answers
C++ question and answersAdenKheire
 
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#)Shoaib Ghachi
 
Technical trainning.pptx
Technical trainning.pptxTechnical trainning.pptx
Technical trainning.pptxSanuSan3
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
cbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxcbybalaguruswami-e-180803051831.pptx
cbybalaguruswami-e-180803051831.pptxSRamadossbiher
 
Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Combating software entropy 2-roc1-
Combating software entropy 2-roc1-Hammad Rajjoub
 
Python Programming Basics for begginners
Python Programming Basics for begginnersPython Programming Basics for begginners
Python Programming Basics for begginnersAbishek Purushothaman
 
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 Featurestechfreak
 

Similar a 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
 

Último

Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniquesugginaramesh
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Último (20)

Comparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization TechniquesComparative Analysis of Text Summarization Techniques
Comparative Analysis of Text Summarization Techniques
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

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

Notas del editor

  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