Sto cercando di capire perché non possiamo usare il tipo annidato attraverso l'espressione di accesso membro della classe. Per esempio, abbiamo la seguente classe:Perché non possiamo usare il tipo annidato attraverso l'espressione di accesso membro della classe?
struct U
{
struct A
{
static int v;
int a;
};
struct B
{
int b;
};
};
U a;
typedef a.A T; //'a' does not name a type
int main()
{
std::cout << typeid(a.A).hash_code(); //invalid use of 'struct U::A'
struct a.A b; //trying to declare a variable of type U::A
//error: expected unqualified-id before '.' token
a.A b; //the same as above
//error: expected unqualified-id before '.' token
a.A.v = 5; //error: expected unqualified-id before '.' token
}
Lo Standard dice:
Sezione N3797::5.2.5/2 [expr.ref]
For the first option (dot) the first expression shall have complete class type. For the second option (arrow) the first expression shall have pointer to complete class type. The expression E1->E2 is converted to the equivalent form (*(E1)).E2; the remainder of 5.2.5 will address only the first option (dot).In either case, the id-expression shall name a member of the class or of one of its base classes.
considerando che la sezione N3797::9.2/1 [class.mem]
dà una definizione di classe membro:
Members of a class are data members, member functions (9.3), nested types, and enumerators.
Quindi non riesco a vedere la restrizione per tale utilizzo di tipo annidato. Perchè no?
Questa è una clausola sotto "* Se E2 è dichiarato di avere tipo" riferimento a T, "*", non penso che questo si applichi qui. – luk32
@ luk32 "Se E2 è dichiarato avere il tipo" riferimento a T ", allora E1.E2 è un lvalue, il tipo di E1.E2 è T. ** In caso contrario, si applica una delle seguenti regole. **" – user657267
Oh L'ho capovolto L'ho letto 3 volte pensando che doveva essere un riferimento a T. Però avrei messo più contesto in risposta. Penso che sia importante avere una regola intera. IMO è questo. – luk32