xml*_*lmx 9 c++ decltype visual-c++ c++11 visual-studio-2012
// 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;
}
};
Run Code Online (Sandbox Code Playgroud)
这是VC++ 2012的错误吗?
这似乎是VS2012编译器错误.规范在第7.1.6.2节第4段中非常清楚.实际上,给出的一个例子显示了一个通过const指针引用的表达式a.decltype(a->x)收益率double,而decltype((a->x))收益率double const &.
所以这是一个错误; 编译器认为i是const,因此不能++.