关键字decltype将变量的类型声明为表达式指定的类型。下面的语句的含义是,让y的类型与x相同,其中x是一个表达式。
1 |
decltype (x) y; |
decltype的工作原理比auto复杂,可以指定类型为引用和const。
atuo关键字不清楚的,看这篇:C++11 atuo关键字
1 2 3 4 5 6 7 8 |
int j = 3; int &k = j; const int &n = j; decltype(n) i1; //i1类型const int& decltype(j) i2; //i2类型 int decltype((j)) i3; //i3类型 int& decltype(k+1) i4; //i4类型 int |
decltype实际运用:
decltype主要运用在定义模板时。因为有很多情况下,只有模板实例化时才能确定是什么类型。
先来看下模板函数例子。
1 2 3 4 5 6 7 8 |
template<typename T,typename U> void fun(T t,U u) { T temp = T*u; //T类型不一定合适 或 U temp = T*u; //U类型不一定合适 ...... } |
比如:T是char,U是short,T*u将是int类型。
decltype就是解决这类问题的,修改后代码。
1 2 3 4 5 6 |
template<typename T,typename U> void fun(T t,U u) { decltype(T*u) tu; ...... } |
You actually expressed it perfectly! Hydrochlorothiazide 50 Mg Tablets Picture
Tips clearly used!. canadian pharmacies online prescriptions
Helpful material. Kudos! hydroxyzine hcl