Title
Static data member in class with name for linkage purposes
Status
cd1
Section
11.4.9.3 [class.static.data]
Submitter
Jorgen Bundgaard

Created on 2003-03-12.00:00:00 last changed 196 months ago

Messages

Date: 2004-03-15.00:00:00

[Voted into WP at March 2004 meeting.]

Date: 2003-10-15.00:00:00

Proposed Resolution (October 2003):

At the end of Clause 11 [class], paragraph 1, add the following:

A class-specifier where the class-head omits the optional identifier defines an unnamed class.

Delete the following from 11.4.9.3 [class.static.data] paragraph 5:

[ Note: this is because there is no mechanism to provide the definitions for such static data members. ]
Date: 2003-10-15.00:00:00

Notes from the October 2003 meeting:

We agree that the example is not valid; this is an unnamed class. We will add wording to define an unnamed class. The note in 11.4.9.3 [class.static.data] paragraph 5 should be corrected or deleted.

Date: 2020-12-15.00:00:00

The following test program is claimed to be a negative C++ test case for "Unnamed classes shall not contain static data members", c.f. ISO/IEC 14882 section 11.4.9.3 [class.static.data] paragraph 5.

  struct B {
         typedef struct {
                 static int i;          // Is this legal C++ ?
         } A;
  };

  int B::A::i = 47;      // Is this legal C++ ?

We are not quite sure about what an "unnamed class" is. There is no exact definition in ISO/IEC 14882; the closest we can come to a hint is the wording of section 9.2.4 [dcl.typedef] paragraph 5, where it seems to be understood that a class-specifier with no identifier between "class" and "{" is unnamed. The identifier provided after "}" ( "A" in the test case above) is there for "linkage purposes" only.

To us, class B::A in the test program above seems "named" enough, and there is certainly a mechanism to provide the definition for B::A::i (in contrast to the note in section 11.4.9.3 [class.static.data] paragraph 5) .

Our position is therefore that the above test program is indeed legal C++. Can you confirm or reject this claim?

Herb Sutter replied to the submitter as follows: Here are my notes based on a grep for "unnamed class" in the standard:

  • 6.6 [basic.link] paragraph 4, bullet 3, makes a note that does not directly speak to your question but which draws the same distinction:
    a named class (clause class), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (9.2.4 [dcl.typedef]);
    Likewise in your example, you have an unnamed class defined in a typedef declaration.
  • 11.4.9.3 [class.static.data] paragraph 5 does indeed appear to me to make your example not supported by ISO C++ (although implementations could allow it as an extension, and many implementation do happen to allow it).
  • 9.2.4 [dcl.typedef] paragraph 5 does indeed likewise confirm the interpretation you give of an unnamed class.

So yes, an unnamed class is one where there is no identifier (class name) between the class-key and the {. This is also in harmony with the production for class-name in Clause 11 [class] paragraph 1:

    class-name:
      identifier
      template-id
History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: wp -> cd1
2004-04-09 00:00:00adminsetmessages: + msg1015
2004-04-09 00:00:00adminsetstatus: ready -> wp
2003-11-15 00:00:00adminsetmessages: + msg914
2003-11-15 00:00:00adminsetmessages: + msg913
2003-11-15 00:00:00adminsetstatus: open -> ready
2003-03-12 00:00:00admincreate