2012-10-16 11 views
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; 
    } 
}; 

Si tratta di un bug di VC++ 2012?È possibile decltype dichiarare un valore r?

+1

I tipi non sono valori r o l-vales; i tipi sono * tipi *. La classificazione del valore-l/valore-r è per * espressioni *. –

+0

Per riferimento, compila sotto gcc 4.6.3 con il flag C++ 0x. Penso che sia giusto, considerando che entrambi i tuoi loop sono identici. –

+0

Il tipo per i in B :: operator == è dedotto come const int, sembra un bug VC. – Andrey

risposta

6

Questo sembra essere un bug del compilatore VS2012. La specifica è abbastanza chiara su questo, nella sezione 7.1.6.2, paragrafo 4. In effetti, uno degli esempi forniti mostra un'espressione che fa riferimento a un puntatore const a. decltype(a->x) produce double, mentre decltype((a->x)) produce double const &.

Quindi è un bug; il compilatore pensa che i sia const e che quindi non sia possibile farlo ++.