[Modern C++] Type Deduction - Template

Program Lang./C++ 2019. 7. 28. 15:16

1. Type Deduction History

 

1) C++98

  • Template Type Deduction

2) C++11

  • Template Type Deduction 변경
  • auto와 decltype 추가

3) C++ 14

  • decltype(auto)가 가능

C++11에서 Template Type Deduction은 auto와 동일하다. 

 

2. Template Type Deduction

 

Effective Modern C++ 기준으로 정리하였으나, 내가 이해한 방식되로 정리하였다. 크게 세가지 관점에서 접근할 수 있다.

 

2.1. ParamType이 T인 경우 - pass by value

 

 

f(expr)에서 expr로 전달되는 원본객체의 const, volatile, reference, pointer를 무시한다.

이 상태에서 T의 타입을 결정하고, expr의 pattern-matching 방식으로 최종 type을 결정한다.

 

f(expr) T type param type Result
f(x) int T f(int param)
f(cx) int T f(int param)
f(rx) int T f(int param)
f(ptr) const char* T f(const char* parm)

 

2.2. ParamType이 참조 또는 포인터인 경우

 

아래 크게 세가지의 경우로 나누어서 정리하였지만, type을 최종 결정하는 방식은

동일한 방식으로 진행된다. 따라서 ParamType을 조금 변경하더라도 동일하게 유추가능하다.

 

1) T&인 경우

 

 

f(expr)에서 expr로 전달되는 원본 객체의 참조를 무시한다.

이 상태에서 T type과 expr과의 pattern-matching 방식으로 최종 type을 결정한다.

 

f(expr) T type param type Result
f(x) int T& f(int& param)
f(cx) const int T& f(const int& param)
f(rx) const int T& f(const int& param)

 

2) const T&인 경우

 

 

f(expr)에서 expr로 전달되는 원본 객체의 상수성과 참조는 무시한다.

이 상태에서 T type과 expr과의 pattern-matching 방식으로 최종 type을 결정한다.

 

f(expr) T type param type Result
f(x) int const T& f(const int& param)
f(cx) int const T& f(const int& param)
f(rx) int const T& f(const int& param)

 

3) ParamType이 포인터인 경우

 

 

f(expr)에서 expr로 전달되는 원본 객체의 포인터는 무시한다.

이 상태에서 T type과 expr과의 pattern-matching 방식으로 최종 type을 결정한다.

 

f(expr) T type param type Result
f(&x) int T* f(int* param)
f(px) const int T* f(const int* param)

 

2.3. ParamType이 foward reference 일 경우 (책에서는 universal reference)

 

 

forward reference의 경우에는 expr로 전달되는 객체가 lvalue인지 rvalue인지를 통해서

T type을 결정하고 param type과는 reference collapsing(책, Item28)을 통해서 최종 타입이 결정된다.

 

f(expr) T type param type Result
f(x)

x : lvalue,

T : int&

T&& f(int& param)
f(cx)

cx : lvalue,

T : const int&

T&& f(const int& param)
f(rx)

rx : lvalue,

T : const int&

T&& f(const int& param)
f(27)

27 : rvalue,

T : int

T&& f(int&& param)

 

3. 추가적인 Type Deduction

 

추가적으로 살펴보아야 할 type deduction은 배열과 함수이다. 이는 원래의 객체의 type이

type deduction 과정 중에 포인터로 바뀌는 현상이 존재하여 이를 잘 구분애서 사용해야 한다.

 

3.1.  배열 Type Deduction

 

배열 type을 갖는 함수 매개변수는 존재하지 않는다.

따라서 T를 통한 type deduction인 f(name)의 경우는 전통적인 C표준에 따라서

const char[13]은 const char*로 변환된다.

 

다만, 배열 type을 그대로 사용하기 위해서는 T&으로 함수템플릿을 정의해야 한다.

 

 

3.2.  함수 Type Deduction

 

위의 배열에서 적용된 타입 변경 방식은 함수에도 동일하게 적용된다.

 

 

4. 정리

 

type deduction에서 pass by value는 expr로 넘겨오는 것은 값으로 복사한다는 측면에서 상수성과 참조성은 무시된다.

reference는 expr로 넘어오는 과정에서 원 객체의 reference를 무시하고 타입 유도를 진행한다.

다만 forward reference는 forward reference 특성을 반영하여 expr로 넘어오는 객체가 lvalue인지 rvalue인지가

타입 유도과정에 적용된다.

 

일반적인 타입 유도뿐만 아니라 전통적인 C에 뿌리를 두고 있는 타입 유도 과정도 같이 고민을 해야 하며,

expr로 넘겨지는 원 객체의 타입을 유지하려면 참조 정의해야 한다.

'Program Lang. > C++' 카테고리의 다른 글

[Modern C++] Type Deduction - decltype  (0) 2019.07.29
[Modern C++] Type Deduction - auto  (0) 2019.07.29
[Basic] Object Generator  (0) 2017.07.09
boost asio 정리 - 5 (work 고찰)  (0) 2017.05.12
boost asio - 정리 4 (strand 적용)  (0) 2017.05.12
: