Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can decltype declare an r-value?

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

like image 345
xmllmx Avatar asked Oct 16 '12 05:10

xmllmx


People also ask

What does decltype do?

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.


1 Answers

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.

like image 195
Nicol Bolas Avatar answered Sep 29 '22 04:09

Nicol Bolas