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?
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
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
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
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
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
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
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.
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