9
// 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?

3
  • 1
    Types aren't r-values or l-vales; types are types. The l-value/r-value classification is for expressions. Oct 16, 2012 at 5:51
  • For reference, it compiles under gcc 4.6.3 with the C++0x flag. I think this is right, considering both your loops are identical. Oct 16, 2012 at 6:02
  • The type for i in B::operator== is deduced as const int, looks like a VC bug.
    – Andriy
    Oct 16, 2012 at 6:04

1 Answer 1

6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.