Mixing class and struct?

It looks to me like it's defined behavior. In particular, §9.1/2 says: A declaration consisting solely of class-key identifier is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name. It introduces the class name into the current scope The standard distinguishes between using class struct or union when defining a class, but here, talking about a declaration, no such distinction is made -- using one class-key is equivalent to any other.

It looks to me like it's defined behavior. In particular, §9.1/2 says: A declaration consisting solely of class-key identifier ; is either a redeclaration of the name in the current scope or a forward declaration of the identifier as a class name. It introduces the class name into the current scope.

The standard distinguishes between using class, struct or union when defining a class, but here, talking about a declaration, no such distinction is made -- using one class-key is equivalent to any other.

That sounds a little presumptuous. "class-key identifier" implies to me the class-key must be the class-key of the class in the same way that the identifier must be the identifier of the class, not just any arbitrary identifier. It doesn't state nor does it seem imply (to me) that all class-keys are equivalent.

– Eric Mickelsen Feb 1 at 19:25 @Eric Mickelsen: I can't agree. It specifically says "the name", which clearly indicates what name is being referred to. Class-key, OTOH, is specifically defined as meaning any of class, struct, or union, and there's nothing here to restrict it to the same one of the three.

– Jerry Coffin Feb 1 at 19:35 1 @eric §7.1.6.3/3 (Elaborated type specifiers, latest C++0x doc I've seen) says "either the class or struct class-key shall be used to refer to a class (Clause 9) declared using the class or struct class-key. " which can be read either way I think. It would only take one word to clarify the meaning of that sentence either way... – awoodland Feb 1 at 19:36 @awoodland: Microsoft chose the "differentiation" road, the Mangling Scheme can be found at en.wikipedia.Org/wiki/…: union is encoded as T, struct as U and class as V.

– Matthieu M. Feb 1 at 19:47 2 @awoodland +1, that is the same quote that I found in the standard. If you read the previous sentences in the same paragraph, my interpretation is that any of struct or class can be used, regardless of which struct or class was used in the declaration: the enum keyword shall be used to refer to an enumeration, the union keyword shall be used to refer to a union, and either class or struct... seen in the context of the full paragraph, my inclination is to think they are interchangeable.

– David Rodríguez - dribeas Feb 1 at 20:37.

In C++, a struct is a class. Specifically: A structure is a class defined with the class-key struct. (ISO/IEC FDIS 14882:1998(E) 9-4) This implies that your class, which was not defined with struct, is definitely not a struct.

Therefore, your forward declaration with the struct class-key is erroneous. I'm not aware of any part of the specification that allows a forward declaration to use a class-key that is clearly wrong. I'm sure that the lenient compilers in question treat structs and classes equally and are glossing over the incorrect declaration.An error may not be required from the compiler in this scenario, but neither should it be unexpected.

I think this is correct. The class name is simply foo, it does not include the struct class-key. However there is no mention in the Standard that says it's OK.

– John Dibling Feb 1 at 18:56 @John Dibling: you might want to read this regarding the difference between foo x; and struct foo x; (Basically, the struct or class keyword there is not part of the name, but an order to the compiler to look into a particular identifier space, that of user defined types) – David Rodríguez - dribeas Feb 1 at 20:20.

From Warning C4099: type name first seen using 'class' now seen using 'struct' (MS VS 2k8) it appears that at least some compilers mangle differently depending on keyword used, so best not to rely on it even if it's technically allowed (of which I can't find a confirming reference).

Technically the code is ok, according to the language standard. However, as at least one of the most popular compilers issues a warning for this, it doesn't work in practice. "In theory, there is no difference between theory and practice.In pratice, there is.

I have no idea whether or not this is undefined (or any of the other categories of not-strictly-conforming) per the C standard, but I do know that if you have two translation units that don't agree on whether a type 'foo' is declared as a 'class' or a 'struct', like so: TU 1 struct foo; void f(foo&) { ... } TU 2 class foo { ... }; void f(foo&); void g() { foo x; f(x); } then, at least some compilers (notably MSVC++) will mangle the name of f differently in each translation unit, so the definition of f in TU 1 does not satisfy the reference to f in TU 2 and you get a link error. This comes up in real life when you have a header A. H that defines class A and needs to refer to classes B, C, and D but a forward declaration of them suffices (so it, quite sensibly, does not include B.

H etc) -- you better use the same keyword for those forward declarations that the actual definitions do!

– awoodland Feb 1 at 18:37 I've definitely seen it with a version more recent than VC6, but I'm not sure how recent. – Zack Feb 1 at 21:46.

MSVC10 throws a warning, and the warning page states that the type given in the definition will be used. msdn.microsoft.com/en-us/library/695x5be....

This doesn't answer the question. The question is, is the behavior UB? – John Dibling Feb 1 at 18:57 The example in the "community content" there looks like it's taken from a standards document, but I can't find it in any of the various documents I have... – awoodland Feb 1 at 19:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions