2012-12-13 18 views
6

Domanda molto semplice. È questo C++ 11 valido?Inizializzatore membro non statico da un altro non statico

struct Foo { 
    int bar = 1; 
    int baz = bar; 
}; 

GCC (4.7.2) e Clang (3.1) sia accettarlo con le impostazioni pedanti:

-std=c++11 -Wall -W -pedantic

Intel C++ (13.0.1.117) non lo fa. E 'abbaia a int baz = bar; con:

error: a nonstatic member reference must be relative to a specific object

Chi ha ragione?

Nel caso in cui ti chiedi, io uso questo per il codice come questo, dove si porta il codice di inizializzazione più vicini, piuttosto che spostare l'ultima riga nel costruttore:

uint8_t colorR = -1; 
uint8_t colorG = -1; 
uint8_t colorB = -1; 
uint8_t colorA = -1; 
GLubyte RGBAVec[4] = {colorR, colorG, colorB, colorA}; 

risposta

3

5.1p12 An id-expression that denotes a non-static data member or non-static member function of a class can only be used:

  • as part of a class member access (5.2.5) in which the object expression refers to the member’s class or a class derived from that class, or
  • to form a pointer to member (5.3.1), or
  • in a mem-initializer for a constructor for that class or for a class derived from that class (12.6.2), or
  • in a brace-or-equal-initializer for a non-static data member of that class or of a class derived from that class (12.6.2), or
  • if that id-expression denotes a non-static data member and it appears in an unevaluated operand.

Quindi sì, questo:

struct Foo { 
    int bar = 1; 
    int baz = bar; 
}; 

valido C++ 11.

Ma fate attenzione su ordine perché:

12.6.2p10 In a non-delegating constructor, initialization proceeds in the following order:

  • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
  • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
  • Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
  • Finally, the compound-statement of the constructor body is executed

Così come specificato nel Non-static data member initializers proposal (Problema 3):

A third issue is that class-scope lookup could turn a compile-time error into a run-time error:

struct S { 
    int i = j; // ill-formed without forward lookup, undefined behavior with 
    int j = 3; 
}; 

(Unless caught by the compiler, i might be intialized with the undefined value of j.)

+0

Grazie. Sembra definitivo. Anche se cosa si intende con "o di una classe derivata da quella classe"? Come è possibile inizializzare un membro con un membro di una classe derivata? La classe derivata non è stata ancora dichiarata, quindi non è possibile accedervi. –

+0

@Nikos C. "o di una classe derivata da quella classe" si riferisce a "un inizializzatore brace-or-equal" che significa che l'inizializzatore è quello che si trova nella classe dervied. Fondamentalmente penso che significhi che puoi inizializzare membri di dati non statici con membri di dati non statici delle tue classi genitore. È il contrario rispetto a quello che hai capito, il che ha molto più senso :) – Drax

Problemi correlati