转载自:http://blog.csdn.net/yshe_xun/article/details/7315135
返回值 decltype(表达式)
[返回值的类型是表达式参数的类型]
这个可也用来决定表达式的类型,就像Bjarne暗示的一样,如果我们需要去初始化某种类型的变量,auto是最简单的选择,但是如果我们所需的类型不是一个变量,例如返回值这时我们可也试一下decltype。
现在我们回看一些例子我们先前做过的,
在下面的一个段落我将会让你熟悉这个观念用 auto 和 decltype 来声明模板函数的返回值,其类型依靠模板参数。
1. 如果这个表达式是个函数,decltype 给出的类型为函数返回值的类型。
2.如果表达式是一个左值类型,那么 decltype 给出的类型为表达式左值引用类型。
3.非常重要的标记一下,decltype 不会执行表达式而auto会,他仅仅推论一下表达式的类型。
跟踪返回类型:
这对 C++ 开发者来说是一个全新的特性,直到现在函数的返回类型必须放在函数名的前面。到了 C++11,我们也可以将函数返回值的类型放在函数声明后,当然仅需要用 auto 替代返回类型。现在我们想知道怎么做,让我们来寻找答案:
我们明显的不能像这样:
这是不是很酷?