1. Trang chủ
  2. » Công Nghệ Thông Tin

Những điểm mới từ C++ tới C++11

52 487 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 52
Dung lượng 1,14 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Những điểm mới của C++11 so với C++. C++11 là một chuẩn mới được cải tiến dựa trên những căn bản của ngôn ngữ lập trình C++....auto variable nullptr Rangebased for loops decltype Uniform Initialization Initializer Lists Override and final Stronglytyped enums Lambdas Smart pointers static_assert and type traits Move semantics rvalue reference thread

Trang 1

From C++ to C++11

Hoàng Mạnh Hưng

hung.hoangmanh2@gameloft.com

May 2015

Trang 2

What is C++11?

Important changes

Getting started

Trang 3

What is C++11?

Important changes

Getting started

3

Trang 4

What is C++11?

• ISO Standard approved on August 2011

• Other name: C++0x

Trang 5

What is C++11?

Important changes

Getting started

5

Trang 7

auto l =[] (int x) -> bool

{ //l has the typeof a

void (*p)(int, float,

char) = foo; // void

foo(int, float, char)

vector<string>::iterator

it = v.begin() // v has

type vector<string>

Trang 8

• New keyword

• Avoid implicit conversion to integral types

• Has type std::nullptr_t

Trang 9

Range-based for loops

Trang 10

Range-based for loops (cont.)

Trang 11

11

• Get type of an expression

• Can be used in definition

}

Trang 12

Uniform Initialization & Initializer Lists

• Initializer lists are not just for arrays

• C++11 allows {}-initializer lists for all

Trang 13

Uniform Initialization & Initializer Lists

C() : arr{1,2,3,4}{} //C++11, member array initializer

C(int i, int j) : a(i), b(j){}

};

void main()

{

C c {0,0}; //C++11 only Equivalent to: C c(0,0);

int* a = new int[3] { 1, 2, 0 }; /C++11 only

}

Trang 14

Uniform Initialization & Initializer Lists

(cont.)

int x1(5.3); // OK, but x1 becomes 5

int x2 =5.3; // OK, but x2 becomes 5

int x3{5.0}; // ERROR : narrowing

int x4 ={5.3}; // ERROR : narrowing

char c1{7}; // OK : even though 7 is an int, this is not

Trang 15

Uniform Initialization & Initializer Lists

f({}); // the empty list

f{1,2}; // error: function call ( ) missing

Trang 16

Override and final

Example: Class D wants to override f() from class B

Trang 17

Override and final (cont.)

17

Solution:

Error

Trang 18

Error

Trang 19

[a,&b] a captured by value, b captured by reference

[this] captures the this pointer by value

[&] captures all automatic variables in the body of

the lambda by reference

[=] captures all  automatic variables in the body of

the lambda by value

[] captures nothing

Trang 20

Lambdas (cont.)

• Examples:

Trang 22

Smart pointers - shared_ptr

• Represent shared ownership of some data

• Is an automatic reference counter

• Allows user-defined deleter

Trang 23

Smart pointers - shared_ptr (cont.)

23

• Problems with arrays: shared_ptr automatic

called delete instead of delete[] when releasing

memory

• Solution:

● Use user-defined deleter

Use default_delete:

• Note: Do not create shared_ptr of array type

std::unique_ptr<int[]> p(new int[10]); //OK

std::shared_ptr<int[]> p(new int[10]); //ERROR: does not compile

Trang 24

Smart pointers - shared_ptr (cont.)

• Misuse of shared-ptr:

• Solution:

Compile OK, but runtime error

Trang 25

Smart pointers - weak_ptr

25

• Holds a reference to an object managed by a

shared_ptr

• Does not contribute to the reference count

• Used to break dependency cycles

Trang 26

Smart pointers - weak_ptr (cont.)

Trang 27

Smart pointers - weak_ptr (cont.)

27

• Example:

Shared pointers never release

Trang 28

Smart pointers - weak_ptr (cont.)

• Solution:

• Different access:

p->mother->kids[0]->name // for shared_ptr

Trang 29

Smart pointers - unique_ptr

29

• Provides strict ownership

• Is not CopyConstructible / CopyConstructible

• Is MoveConstructible / MoveAssignable

• Usage:

● Providing exception safety

● Passing ownership to a function

● Returning ownership from a function

● Storing pointers in containers

Trang 30

Smart pointers - unique_ptr (cont.)

• Example:

• Solution:

Or:

Exception unsafe

Trang 31

Smart pointers - unique_ptr (cont.)

31

• Allocate and release:

• Dealing with arrays:

Trang 32

Smart pointers - unique_ptr (cont.)

• Using deleter:

Trang 33

static_assert and type traits

33

static_assert performs an assertion check at

compile-time

Trang 34

static_assert and type traits (cont.)

<type_traits> defines a series of classes to

obtain type information on compile-time:

● Helper classes

● Type traits

● Type transformations

Trang 35

Move semantics & rvalue reference

35

• Example:

● It may be expensive to copy T

● We only want to “move” the values around

Trang 36

Move semantics & rvalue reference

(cont.)

• Solution:

• “move constructors” and “move

assignments”:

Trang 37

37

• Allow multiple pieces of code to run

asynchronously and simultaneously

Trang 38

thread - mutex

• std::mutex protect shared data from being

simultaneously accessed by multiple threads

Trang 39

Thread 2

Trang 40

thread - atomic (cont.)

Trang 41

thread - atomic (cont.)

41

• Solution:

Trang 42

What is C++11?

Important changes

Getting started

Trang 45

Visual Studio

45

• VS 2012 Express with

• Some features may not available yet

Trang 46

Visual Studio (cont.)

Trang 47

Try it online

47

Trang 48

In GL projects?

• Add to CFLAGS in sln2gcc.xml:

-std=c++11

• Re-build

Trang 49

49

Trang 51

51

Trang 52

THANK YOU!

Ngày đăng: 12/04/2016, 23:59

TỪ KHÓA LIÊN QUAN

w