c++ 타입 추론

20
C++ 타입 추론 클라이언트 프로그래머 박재완 template, auto, [](){};

Upload: huey-park

Post on 20-Jul-2015

418 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: C++ 타입 추론

C++ 타입 추론

클라이언트 프로그래머

박재완

template, auto, [](){};

Page 2: C++ 타입 추론

조금만 알아보고 시작합시다

Value C++ 11 이전

lvalue : 표현식 이후에도 지속되는 값

rvalue : 표현식 종료 이후에는 더이상

존재하지 않는 값

int Function();

int x = 3;

int y = Function();

Page 3: C++ 타입 추론

조금만 알아보고 시작합시다

Value C++ 11 이후

lvalue : C++ 11 이전의 lvalue

prvalue : C++ 11 이전의 rvlaue

xvalue : 생명주기가 도래해가는 값,

rvlaue reference와 함께 등장,

"eXpiring value"의 약자

rvalue : xvalue + prvalue

glvalue : lvlaue + xvalue

Page 4: C++ 타입 추론

조금만 알아보고 시작합시다

Universal Reference

template<typename T>

void f(T&& param)

auto&& x;

x는 rvalue reference가 아닙니다.

Page 5: C++ 타입 추론

이제부터 타입 추론

C++ 98

template

C++ 11

auto, universal reference,

lambda capture and return, decltype

C++ 14

lambda init captures, function return

Page 6: C++ 타입 추론

왜 알아야 하는가?

class Button

{

virtual void OnClicked();

}

class Button2 : public Button

{

virtual void OnClicked() override;

}

Button3, Button4 ...

Page 7: C++ 타입 추론

왜 알아야 하는가?

class Button

{

void OnClicked() { this->_function(); }

void SetFunction(typedef std::function<void()> function)

{

this->_function = function;

}

std::function<void()> _function;

}

Page 8: C++ 타입 추론

왜 알아야 하는가?

Data data = GetSomeData();

Button button;

auto OnClicked = [data]()

{

DoSomethingWithData(data);

...

};

button.SetFunction(OnClicked);

Page 9: C++ 타입 추론

여기서 문제!

Data GetData(int iID)

{

this->_mapData.find(iID)->second;

}

auto& iter = GetData(iID);

Page 10: C++ 타입 추론

Template 타입 추론

template <typename T>

void f(T param);

f(expr);

expr??

T가 레퍼런스나 포인터이고 universal reference가 아닐 때

T가 universal reference 일 때

T가 레퍼런스나 포인터가 아닐 때

Page 11: C++ 타입 추론

Template 타입 추론

레퍼런스나 포인터이고 universal reference가 아닐 때

template <typename T>

void f(T& param);

int x = 22;

const int cx = x;

const int& rx = x;

f(x); // int&

f(cx); // const int&

f(rx); // const int&

Page 12: C++ 타입 추론

Template 타입 추론

레퍼런스나 포인터이고 universal reference가 아닐 때

template <typename T>

void f(const T& param);

int x = 22;

const int cx = x;

const int& rx = x;

f(x); // const int&

f(cx); // const int&

f(rx); // const int&

Page 13: C++ 타입 추론

Template 타입 추론

universal reference 일 때

template <typename T>

void f(T&& param);

int x = 22;

const int cx = x;

const int& rx = x;

f(x); // int&

f(cx); // const int&

f(rx); // const int&

f(22); // int&&

Page 14: C++ 타입 추론

Template 타입 추론

레퍼런스나 포인터가 아닐 때

template <typename T>

void f(T param);

int x = 22;

const int cx = x;

const int& rx = x;

f(x); // int

f(cx); // int

f(rx); // int

Page 15: C++ 타입 추론

auto 타입 추론

레퍼런스나 포인터이고 universal reference가 아닐 때

int x = 22;

const int cx = x;

const int& rx = x;

auto& v1 = x; // int&

auto& v2 = cx; // const int&

auto& v3 = rx; // const int&

const auto& v1 = x; // const int&

const auto& v2 = cx; // const int&

const auto& v3 = rx; // const int&

Page 16: C++ 타입 추론

auto 타입 추론

universal reference 일 때

int x = 22;

const int cx = x;

const int& rx = x;

auto&& v1 = x; // int&

auto&& v2 = cx; // const int&

auto&& v3 = rx; // const int&

auto&& v4 = 27; // int&&

Page 17: C++ 타입 추론

auto 타입 추론

레퍼런스나 포인터가 아닐 때

int x = 22;

const int cx = x;

const int& rx = x;

auto v1 = x; // int

auto v2 = cx; // int

auto v3 = rx; // int

Page 18: C++ 타입 추론

auto 타입 추론

Template 타입 추론과 동일함

하지만 braced initializer!

template <typename T>

void f(T param);

f( { 1, 2, 3 } ); // error!

auto x1 { 1, 2, 3 }; // std::intializer_list<int>

auto x2 = { 1, 2, 3 }; // std::intializer_list<int>

Page 19: C++ 타입 추론

Lambda Capture 타입 추론

By reference : template과 동일

By value : template과 동일하지만 cv 키워드가 유지

const int cx = 0;

auto lam = [cx] {...}

class UpToCompiler

{

private:

const int cx;

...

}

Page 20: C++ 타입 추론

아까 그 문제!

Data GetData(int iID)

{

this->_mapData.find(iID)->second;

}

auto& iter = GetData(iID);