学习中……
WHAT
decltype用于缀取一个表达式的类型,并且通过decltype获取到的类型可以用于声明变量。decltype的语法如下:
unary-expression
...
decltype(unary-expression)
decltype(type-id)
typedef decltype(unary-expression) temp
...
下面是从文档Decltype and auto中摘取出来的,关于decltype更加详细的描述:
1. If e is a name of a variable in namespace or local scope, a static member variable, or a formal parameter of a function, decltype(e) is the declared type for that variable or formal parameter. Particularly, decltype(e) results in a reference type only if the variable or formal parameter is declared as a reference type.
2. If e is an invocation of a function or operator, either user-defined or built-in, decltype(e) is the declared return type of that function. The standard text does not list the prototypes of all built-in operators. For the operators and expressions whose prototypes are not listed, the declared type is a reference type whenever the return type of the operator is specified to be an lvalue.
3.decltype does not evaluate its argument expression.
4. The decltype taking a type parameter is an identity function: decltype(T) is equal to T for any type expression T.
HOW
auto x = 4;//x是类型int
decltype(x) y = x;//相当于auto y = x;或者int y = x;
auto add(int x, int y) ->decltype(x + y);//很有意思的用法
IDIOMS
PROBLEMS
引用
1. http://www.stroustrup.com/C++11FAQ.html#decltype
2. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1478.pdf
3. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2343.pdf
4.http://cpp-next.com/archive/2011/04/appearing-and-disappearing-consts-in-c/