c++ 타입 추론
TRANSCRIPT
C++ 타입 추론
클라이언트 프로그래머
박재완
template, auto, [](){};
조금만 알아보고 시작합시다
Value C++ 11 이전
lvalue : 표현식 이후에도 지속되는 값
rvalue : 표현식 종료 이후에는 더이상
존재하지 않는 값
int Function();
int x = 3;
int y = Function();
조금만 알아보고 시작합시다
Value C++ 11 이후
lvalue : C++ 11 이전의 lvalue
prvalue : C++ 11 이전의 rvlaue
xvalue : 생명주기가 도래해가는 값,
rvlaue reference와 함께 등장,
"eXpiring value"의 약자
rvalue : xvalue + prvalue
glvalue : lvlaue + xvalue
조금만 알아보고 시작합시다
Universal Reference
template<typename T>
void f(T&& param)
auto&& x;
x는 rvalue reference가 아닙니다.
이제부터 타입 추론
C++ 98
template
C++ 11
auto, universal reference,
lambda capture and return, decltype
C++ 14
lambda init captures, function return
왜 알아야 하는가?
class Button
{
virtual void OnClicked();
}
class Button2 : public Button
{
virtual void OnClicked() override;
}
Button3, Button4 ...
왜 알아야 하는가?
class Button
{
void OnClicked() { this->_function(); }
void SetFunction(typedef std::function<void()> function)
{
this->_function = function;
}
std::function<void()> _function;
}
왜 알아야 하는가?
Data data = GetSomeData();
Button button;
auto OnClicked = [data]()
{
DoSomethingWithData(data);
...
};
button.SetFunction(OnClicked);
여기서 문제!
Data GetData(int iID)
{
this->_mapData.find(iID)->second;
}
auto& iter = GetData(iID);
Template 타입 추론
template <typename T>
void f(T param);
f(expr);
expr??
T가 레퍼런스나 포인터이고 universal reference가 아닐 때
T가 universal reference 일 때
T가 레퍼런스나 포인터가 아닐 때
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&
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&
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&&
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
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&
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&&
auto 타입 추론
레퍼런스나 포인터가 아닐 때
int x = 22;
const int cx = x;
const int& rx = x;
auto v1 = x; // int
auto v2 = cx; // int
auto v3 = rx; // int
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>
Lambda Capture 타입 추론
By reference : template과 동일
By value : template과 동일하지만 cv 키워드가 유지
const int cx = 0;
auto lam = [cx] {...}
class UpToCompiler
{
private:
const int cx;
...
}
아까 그 문제!
Data GetData(int iID)
{
this->_mapData.find(iID)->second;
}
auto& iter = GetData(iID);