MBZ*_*MBZ 34 c++ decltype c++11
我正在维基百科上C++11
阅读关于类型推断功能的这篇文章.
有一个例子,我引述:
#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
}
Run Code Online (Sandbox Code Playgroud)
在以下几行中:
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
Run Code Online (Sandbox Code Playgroud)
c
和之间有什么区别(c)
?为什么(c)
代表左值?
Lig*_*ica 21
c
是变量的名称;
(c)
是一个表达式,在本例中是一个左值表达式,其值与变量的值相同c
.
两者的处理方式不同decltype
.例如,考虑一下,decltype(1+2)
这也是一个表达式的例子.碰巧的是,你的例子是一个表达式的简单版本:一个只是命名一个变量,并且没有什么令人兴奋的.
如果您对语言规范的细微部分进行合理化,那么您通常只关心这些差异之一; 但是,正如您所确定的那样,在这种情况下它具有非常重要的实际效果.
请注意,这里没有操作员使用.这只是语法布局的一个推论.
我在这里找到了很好的描述.它描述了以下区别:
struct A { double x; };
const A* a = new A();
...
decltype(a->x) x4; // type is double
decltype((a->x)) x5; // type is const double&
Run Code Online (Sandbox Code Playgroud)
我引述:
后两个decltype调用之间差异的原因是括号表达式
(a->x)
既不是id表达式也不是成员访问表达式,因此不表示命名对象.[ 13 ]因为表达式是左值,其推导类型是"对表达式类型的引用",或
const double&
.[ 10 ]
归档时间: |
|
查看次数: |
3421 次 |
最近记录: |