decltype (C++11)
decltype 키워드는 특정 표현식의 타입을 알려줍니다.
decltype | (expression) |
---|
#include <iostream>
int main(int argc, const char * argv[])
{
int lhs = 10;
int rhs = 20;
decltype (lhs+rhs) result = lhs+rhs;
printf("%d\n",result);
return 0;
}
decltype(lhs+rhs)
int + int = int 이기 때문에 int 가 추론되고, result 는 int type 변수로 선언됩니다.
만약 위의 코드에서 lhs 와 rhs 를 float 형으로 변경한다면, result 역시 float 형으로 바뀌어서 추론됩니다.
decltype 은 문법상 타입을 명시하는 곳이라면 어디든지 위치할 수 있습니다.
이 기능은 다른 장에서 설명할 auto keyword 와 같이 사용하거나, template 을 작성하는 데에 매우 유용하게 사용할 수 있습니다.
decltype 의 추론(deduce) 규칙은 template 의 argument deduction 과 다르다는 점에 주의
할 필요가 있습니다.