// 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;
}
};
Is this a bug of VC++ 2012?
This appears to be a VS2012 compiler bug. The spec is quite clear on this, in section 7.1.6.2, paragraph 4. Indeed, one of the examples given shows an expression that references through a const-pointer
a.decltype(a->x)yieldsdouble, whiledecltype((a->x))yieldsdouble const &.So it’s a bug; the compiler thinks that
iisconst, and therefore can’t++it.