[c++ korea] effective modern c++ study item 1 understand template type deduction +정은식

13
Effective Modern C++ Study C++ Korea C++ Korea Effective Modern C++ Study Item 1 – 3 Item 1 : Understand template type deduction. Item 2 : Understand auto type deduction. Item 3 : Understand decltype.

Upload: seok-joon-yun

Post on 14-Jul-2015

243 views

Category:

Software


4 download

TRANSCRIPT

Page 1: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

C++ Korea Effective Modern C++ Study

Item 1 – 3

Item 1 : Understand template type deduction.

Item 2 : Understand auto type deduction.

Item 3 : Understand decltype.

Page 2: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Item 1 : Understand template type

deduction.

Speaker : 정은식

Page 3: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Deducing Types

C++ 11에서의 auto는 template 기반으로 한다.

떄문에 템플릿에 적용할 떄보다 덜 직관적으로 보인다

템플릿에는 추론 방법이 3가지가 있다.

3

Page 4: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Deducing Types

컴파일을 하는 동안 컴파일러는 T및 paramType

두 타입을 추론하기 위해 expr 을 사용합니다.

4

Page 5: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 1: ParamType 이 참조, 포인터인 경우

Case 2: ParamType 가 Universal Reference 경우

Case 3: ParamType 가 참조 또는 포인터가 아닐떄

5

Page 6: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 1

ParamType 이 참조거나 포인터인 경우

1)expr 타입이 참조 타입일떄 참조 부분은 무시됩니다.

2)expr 타입과 매치한 후 paramtype으로 T를 결정합니다

6

Page 7: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 1

void f(T& param); // param is a reference

int x = 27;

const int cx = x;

const int& rx = x;

F(x); // param int&

F(cx); //param's type is const int&

F(rx); //param's type is const int&

7

Page 8: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 1

template<typename T>

void f(T* param);

int x = 27;

const int *px = &x

f(&x); // param's type is int*

f(px); //param's type is const int*

8

Page 9: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 2:

ParamType 가 Universal Reference 경우

1.If expr is an lvalue 1) T와 paramtype은 lvalue 의해 추론.

2) 비록 paramtype이 rvalue 선언되어있어도 참조lvalue 로 참조

2.expr이 rvalue 일떄 case 1가 동일하게 적용

9

Page 10: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 2:

void f(T&& param);

int x = 27;

const int cx = x;

const int& rx = x;

F(x); F(cx);

F(rx); F(27);

10

Page 11: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 3:

Case 3: ParamType 가 참조,포인터가 아닐떄

expr이 reference ,const,volatile 다 무시됩니다.

11

Page 12: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

Case 3:

void f(T param);

int x = 27;

const int cx = x;

const int& rx = x;

f(x); // T's and param's types are both int

f(cx); // T's and param's types are both int

f(rx); // T's and param's types are both int

12

Page 13: [C++ korea] effective modern c++ study   item 1 understand template type deduction +정은식

Effective Modern C++ Study

C++ Korea

정리 템플릿 타입 추론을 하는동안. 레퍼런스 타입의 인수들은 레퍼런스가 아닌 타입으로 취급

유니버셜 레버런스에 대한 타입 추론을 할떄 Lvalue 인수들은 특별한 취급을 받게됨

템플릿 타입 추론을 하는동안 포인터를 사용하는 인수들은 초기화할떄 레퍼런스 타입을 사용하지 않으면 포인터 붕괴가 일어남

13