c++11

58
New in C++11 Trần Duy Quang – May 2014

Upload: tran-quang

Post on 17-Jul-2015

122 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: C++11

New in C++11

Trần Duy Quang – May 2014

Page 2: C++11

Agenda

1. Automatic variables

2. Decltype

3. Rvalue reference

4. Lambda function

5. Variadic template

6. Concurrency library

2

Page 3: C++11

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!

Page 4: C++11

auto Variables

Implicit variable declaration

Keyword: auto

4Note: cbegin to iterate constant, no change in content

Page 5: C++11

Careful: auto default

By-value for references

Use auto& or decltype for desired result!

5

int f, not int& f!

Page 6: C++11

More mindblow

int&: auto is int (only, no &)

But int*: auto is int* !

6

Page 7: C++11

std::bind

Placeholder objects _1, _2, .., _N

C# alike

7

1. May cause runtime error, but when?

2. What about _10 rather than _2?

Page 8: C++11

Range-based for loop

8

Better? auto&!

Page 9: C++11

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?

Page 10: C++11

auto without decltype

Compile error

10

Page 11: C++11

Why auto & decltype?

11

Page 12: C++11

Parenthesized variable

Always a reference!

12

Page 13: C++11

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:

Page 14: C++11

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) )

Page 15: C++11

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

Page 16: C++11

More mindblow (again)

decltype(auto) f = expression; // C++14

C++17 will go further? No ideas!

16

Page 17: C++11

Lambda function

17

Page 18: C++11

Lambda function

[]: capture list

[] () -> return type

18

Page 19: C++11

Lambda capture

Capture by value

42

Capture by reference

43

43

(Can be modify inside, but not affect outside)

19

Page 20: C++11

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

Page 21: C++11

Recursive lambda

21

Page 22: C++11

Stateless lambda

Convertible to function pointer!

Used with C-style API (e.g. Win32)

22

Page 23: C++11

High-order function

Takes one or more functions as an input

Outputs a function

23

Sắp xếp tăng dần

Page 24: C++11

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

Page 25: C++11

Let’s see one generic lambda

25Did I tell you about functor?

Page 26: C++11

Lambda physiology

For each lambda compiler generate a class

Specifying lambda instantiate an object

Invoking lambda call the object’s operator()

26

Page 27: C++11

Rvalue reference

27

Page 28: C++11

Rvalue definition

lvalue: can appear on the left and right

rvalue: can only appear on the right

28

Page 29: C++11

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

Page 30: C++11

Obvious solution

Overload operator= ! (Copy assignment)

Right hand side should be passed by reference

rvalue references!

MyString&&: rvalue reference to MyString&

30

Page 31: C++11

Function overload resolution

“Am I being called on an lvalue or rvalue?”

31

Page 32: C++11

Achieving move semantics

Should occur only for copy constructor and

assignment operator!

32

Page 33: C++11

Forcing move semantics

std::move help

Type with no move semantics implementation?

Swap like old times!

33

Page 34: C++11

Funny jokes

34

Page 35: C++11

Smart pointers

35

Page 36: C++11

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

Page 37: C++11

Before we go on

Resource Acquisition Is Initialization (RAII)

37

Page 38: C++11

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

Page 39: C++11

unique_ptr

39

Page 40: C++11

shared_ptr

Last pointer dies, object is deleted

40

Page 41: C++11

weak_ptr

Does not affect reference count

Break cycles between shared_ptrs

41

Page 42: C++11

Some other things

42

Page 43: C++11

Threads & Async

43

Page 44: C++11

Variadic template

44

Page 45: C++11

Rules of expansion

45

Page 46: C++11

Initializer list

46

Page 47: C++11

using

47

Page 48: C++11

Raw string literals

48

Page 49: C++11

Other examples of raw string literals

49

Page 50: C++11

Tuple

50

Page 51: C++11

Default & delete

51

Page 52: C++11

The last coffee drop

52

Page 53: C++11

C++ Accelerated Massive

Parallelism

53

Page 54: C++11

Simple example

54

Normal version AMP version

Page 55: C++11

Basic elements of AMP

55

Code run on each thread

Thread id running the

lambda

Check if code can run with AMP / Direct3D

Page 57: C++11

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

Page 58: C++11

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