我正在读书维基百科上的这篇文章关于C++11 类型推断 feature http://en.wikipedia.org/wiki/C++11#Type_inference.
有一个例子,我引用一下:
#include <vector>
int main() {
const std::vector<int> v(1);
auto a = v[0]; // a has type int
decltype(v[1]) b = 1; // b has type const int&, the return type of
// std::vector<int>::operator[](size_type) const
auto c = 0; // c has type int
auto d = c; // d has type int
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
decltype(0) g; // g has type int, because 0 is an rvalue
}
在以下几行中:
decltype(c) e; // e has type int, the type of the entity named by c
decltype((c)) f = c; // f has type int&, because (c) is an lvalue
有什么区别c
and (c)
?为什么(c)
代表一个lvalue?
并且两者的待遇是不同的decltype
。例如,考虑一下,decltype(1+2)
,这也是一个例子表达。碰巧你的例子是simple表达式的版本:仅命名单个变量且不执行任何令人兴奋的操作的版本。
这是您通常只有在合理化语言规范的微妙部分时才真正关心的差异之一;不过,正如您所指出的,它在本例中具有相当显着的实际效果。
请注意,虽然没有operator用法在这里。这一切都只是从语法布局中的推论。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)