Modern Effective C++ Item2 Understanding Auto type deducing

18
Effective Modern C++ Study C++ Korea C++ Korea Effective Modern C++ Study Chapter 1 Deducing Types Item 1: Understand template type deduction Item 2: Understand auto type deduction. Item 3: Understand decltype.

Transcript of Modern Effective C++ Item2 Understanding Auto type deducing

Effective Modern C++ StudyC++ Korea

C++ KoreaEffective Modern C++ Study

Chapter 1 Deducing TypesItem 1: Understand template type deductionItem 2: Understand auto type deduction. Item 3: Understand decltype.

Item 2 : Understand auto

type deduc-ing

Effective Modern C++ StudyC++ Korea3

Before start.. Item 1 에서 설명한 간단한 Type Deducing 규칙Case1. Pointer or reference but not universal reference

expr 의 reference 는 무시된다 . const int & 를 추론 할 경우 const int 형식으로 추론된다 .

Case 2. Universal reference

Lvalue 일 경우 reference type, Rvalue 일 경우 rvalue reference로 추론

Case 3. Neither a Pointer nor a Reference

Reference, const, volatile 등 다 무시한다 .

Auto type deducing

Effective Modern C++ StudyC++ Korea5

auto is C++11 에서 추가된 키워드선언된 변수의 초기화 식을 이용하여 컴파일타임에

해당 형식을 추론하는 키워드 .

긴 STL 구문을 줄일 때 아주 편하다 .

int a = 27;

auto b = a; (int 형식으로 추론해준다 .)

Effective Modern C++ StudyC++ Korea6

auto 와 template 사이auto tem-

platedirect mapping

auto 타입추론은 template 타입추론과 같다 .auto 와 template 사이의 direct mapping 을 통해같은 방식으로 추론한다 .( 한 가지 예외 제외 )

type deducing type deducing

Effective Modern C++ StudyC++ Korea7

direct mapping

template<typename T>void func(ParamType param);func(expr);template : expr 을 이용해 ParamType 과 T 를 타입 추론한다 .

auto : auto 가 템플릿에서 T 의 역할을 , 형식 지정자 (type speci-fier) 가 ParamType 의 역할을 한다 . 같은 방식으로 타입 추론한다 .

auto param = expr;

Effective Modern C++ StudyC++ Korea8

direct mappingconceptional template

template<typename T>void func_for_x(T param);

func_for_x(27);

template<typename T>void func_for_cx(const T param);

func_for_cx(27);

template<typename T>void func_for_rx(const T& param);

func_for_rx(27);

auto 로 변수 선언auto x = 27;

const auto cx = x;

const auto & rx = x;

Effective Modern C++ StudyC++ Korea9

direct mappingconceptional template

template<typename T>void func_for_x(T param);

func_for_x(27);

auto 로 변수 선언auto x = 27;

컴파일러가 하는 일auto 로 변수를 선언 -> 각각 맞는 템플릿을 호출 ->

템플릿 타입추론 -> 추론한 형식에 맞는 initializing expression 호출

Effective Modern C++ StudyC++ Korea10

auto type deduction Item1 에서 나눴던 3 가지 case 들을 auto 에 대입해보자

case 1 & case 3- auto x = 27; //case 3 (x is neither ptr nor reference)- const auto cx = x; //case 3 (cx is neither ptr nor refer-

ence)- const auto& rx = x; //case 1 (rx is a non-universal

reference)case 2- auto&& uref1 = x; //(x 가 lvalue, int | uref1 는 int&)- auto&& uref2 = cx; //(cx 가 lvalue, const int | uref2 는

const int &)- auto&& uref3 = 27; //(27 이 rvalue, int | uref3 는

int&&)

Effective Modern C++ StudyC++ Korea11

auto(exception) C++11 에서 변수를 초기화 할 수 있는 방법들

int x1 = 27;

int x2(27);

//C++11 에 추가된 Uniform Initialization

int x3 = {27};

int x4{27};

Effective Modern C++ StudyC++ Korea12

auto(exception) auto 를 쓴다면 ?

auto x1 = 27; -> int x1;

auto x2(27); -> int x2;

//C++11 에 추가된 Uniform Initialization

auto x3 = {27}; -> class std::initializer_list<int>

x3; value 27

auto x4{27}; -> class std::initializer_list<int> x4;

value 27

auto x5{1, 2, 3.0} -> compile error(type deduce fail)

Effective Modern C++ StudyC++ Korea13

auto(exception)

auto 의 특별한 타입추론 규칙 때문1. auto 를 초기화 할 때 uniform-initializer 를 사용 할 경우

std::initializer_list<int> 로 추론된다 .

2. braced initializer( { … } ) 안에 서로 다른 타입들이 들어갈 경우 타입 추론을 할 수 없어서 오류가 난다 .

auto 를 uniform-initializer 로 초기화 할 경우 실제로 타입추론이 두 번 일어나게 된다 .auto 가 initializer_list 를 추론 할 때와 initializer_list 자체는

std::lintializer_list<T>임으로 같은 타입의 변수가 들어가지 않으면 안 된다 .

Effective Modern C++ StudyC++ Korea14

auto(exception) auto 와 template 의 다른점은 ?template<typename T>void function(T param);f( { 11, 2, 7 } ); //Error! 타입 추론 실패auto 는 brace intializer 을 std::initializer_list<int> 로 추론 하지만template 은 추론을 하지 못한다 .template<typename T>void function(std::initializer_list<T> param);f( { 11, 2, 7 } ); // 다음과 같은 식으로 하면 추론 가능

Effective Modern C++ StudyC++ Korea15

auto(exception) 왜 auto 는 std::initializer_list 로 잘 추론하는데 template 은 아닌가요 ?

Just because아직 스콧마이어 아저씨도 적절한 이유를 못 찾았다고 합니다

Effective Modern C++ StudyC++ Korea16

auto in C++14 function return type 에 auto 사용

auto CreateInitList(){

return { 1, 2, 3}; //error 타입 추론 실패}

lambda 에서 parameter 선언시 auto 사용가능std::vector<int> v;auto resetV = [&v](const auto& newValue) { v = newValue; };resetV( {1, 2, 3 } ); //error type 추론 실패

auto 를 return type 이나 lambda parameter 에 쓸 경우 auto 보다는 tem-plate 에 가깝다

Effective Modern C++ StudyC++ Korea17

마지막 정리 auto 타입추론은 template 타입추론 방식과 대부분 같다 . 하지만

braced initializer list( {…} ) 를 auto 는 std::initializer_list 로 추론하지만 template 은 추론 하지 못한다

auto 를 함수 리턴타입에 쓰거나 lambda 함수의 변수 선언에 쓸 경우 auto 타입추론 보다는 template 타입추론에 가깝다 .

Effective Modern C++ StudyC++ Korea18

Q&A