// 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?
The decltype type specifier enables generic forwarding functions because it does not lose required information about whether a function returns a reference type. For a code example of a forwarding function, see the previous myFunc template function example.
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)
yields double
, while decltype((a->x))
yields double const &
.
So it's a bug; the compiler thinks that i
is const
, and therefore can't ++
it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With