Upload
tran-quang
View
122
Download
4
Tags:
Embed Size (px)
Citation preview
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
4Note: cbegin to iterate constant, no change in content
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
91Algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters
Why do we have to use auto & decltype here?
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 xType 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
25Did 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