// Compiled by Visual Studio 2012
struct A
{
bool operator ==(const A& other) const
{
for (decltype(this->n) i = 0; i < n; ++i) // OK
{}
return true;
}
protected:
size_t n;
};
struct B : public A
{
bool operator ==(const B& other) const
{
for (decltype(this->n) i = 0; i < n; ++i) // error C2105: '++' needs l-value
{}
return true;
}
};
这是 VC++ 2012 的错误吗?
最佳答案
这似乎是一个 VS2012 编译器错误。规范在 7.1.6.2 节的第 4 段中对此非常清楚。实际上,给出的示例之一显示了通过常量指针 a
引用的表达式。 decltype(a->x)
产生 double
,而 decltype((a->x))
产生 double const &
.
所以这是一个错误;编译器认为 i
是 const
,因此不能 ++
它。
关于c++ - decltype 可以声明一个右值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12908297/