2

I read this about class in the C++ standard document:

A class is a type. Its name becomes a class-name (9.1) within its scope.

class-name: identifier template-id

I found this grammar for an identifier in the C++ Standard:

 2.10 Identifiers
 identifier: nondigit
 identifier nondigit
 identifier digit

 nondigit: one of universal-character-name 
 _ a b c d e f g h i j k l m n o p q r s t u  v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
 digit: one of 0 1 2 3 4 5 6 7 8 9

Now I tried doing this:

class
{
public:
  int i;
};

and it compiles fine without any name.

Can anyone give me an explanation regarding this? Isn't it a violation of the grammar specified for an identifier?


Nawaz had asked a follow up question regarding the standard compliance of the code I had given. Those interested can check it out here.

Community
  • 1
  • 1
Amit Tomar
  • 4,800
  • 6
  • 50
  • 83

4 Answers4

7

The grammar goes

class-specifier:
    class-head { member-specification_opt }

class-head:
    class-key attribute-specifier-seq_opt class-head-name class-virt-specifier-seq_opt base-clause_opt
    class-key attribute-specifier-seq_opt base-clause_opt

class-key:
    class
    struct
    union

In your case, the second production of class-head is used -- no class-name is involved.

avakar
  • 32,009
  • 9
  • 68
  • 103
0

The identifier is omitted entirely, so the question of correct grammar for the identifier is moot. Nothing in the description says the identifier must be present. Anonymous classes are probably allowed for consistency with C struct rules, which allows constructs such as:

typedef struct { int i; } Foo;

struct { int x, y; } points[] = { {1, 2}, {3, 4} };

I don't think I've ever seen this done for a class.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • -1, the description he quotes says that an identifier must be present. – Johannes Schaub - litb Oct 30 '12 at 09:54
  • @JohannesSchaub-litb: that quote is irrelevant as far as the posted code is concerned. – Nawaz Oct 30 '12 at 10:09
  • you are saying "the description". i must be assuming you refer to the question. – Johannes Schaub - litb Oct 30 '12 at 10:15
  • @JohannesSchaub-litb: Yes, I was referring to the quotations in the question. I can find nothing (quoted or otherwise) in any version of the question that says anything resembling "a class must have a name". What am I missing? I suppose I'll slap my forehead when you point it out, but I just can't see it. – Marcelo Cantos Oct 30 '12 at 23:05
0
class {public: int i;}

is quite useless, but you can specyfy a class without a name and then create instances of this class. Knowing that you can use the following:

class {public: int i;} a,b,c;
a.i = 5;
b.i = 4;
c.i = 3;
cout<<a.i<<" "<<b.i<<" "<<c.i;

You can use this inside a function too (as an anonimous class), so knowing that you can use something like this:

void x()
{
    class {public: int i;} a,b,c;
    a.i = 5;
    b.i = 4;
    c.i = 3;
    cout<<a.i<<" "<<b.i<<" "<<c.i;
}

int main() {
    x();
}
-1

The code class { int i; }; is fully-standard conformant.You've quoted the irrelevant reference from the Standard which has nothing to do with anonymous class.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • @JohannesSchaub-litb : `class { public: int i; };` is not fully standard conformant? why? – Nawaz Oct 30 '12 at 10:07
  • @JohannesSchaub-litb: Could you please tell me what is wrong with `class { public: int i; };`? Why is not Standard conformant? (I'm waiting for your answer, so I may know it myself). – Nawaz Oct 30 '12 at 10:41
  • 1
    while it is grammatically valid, it breaks the rule that such a class must declare at least one name into its enclosing scope. – Johannes Schaub - litb Oct 30 '12 at 11:37
  • @JohannesSchaub-litb: I didn't get it. What name are you talking about? Could you elaborate on it? – Nawaz Oct 30 '12 at 11:43
  • you may want to ask a Stackoverflow question if you are interested in elaborated explanations. These comment boxes are too small for that. – Johannes Schaub - litb Oct 30 '12 at 11:48
  • @JohannesSchaub-litb: Done. http://stackoverflow.com/questions/13138605/why-class-int-i-is-not-fully-standard-conformant – Nawaz Oct 30 '12 at 12:05