I'm well aware of the difference between class and struct, however I'm struggling to authoritatively say if this is well defined:
// declare foo (struct)
struct foo;
// define foo (class)
class foo {
};
// instance of foo, claiming to be a struct again! Well defined?
struct foo bar;
// mixing class and struct like this upsets at least one compiler (names are mangled differently)
const foo& test() {
return bar;
}
int main() {
test();
return 0;
}
If this is undefined behaviour can someone point me in the direction of an authoritative (i.e. chapter and verse from ISO) reference?
The compiler with problems handling this (Carbide 2.7) is relatively old and all the other compilers I've tried it on are perfectly happy with this, but clearly that doesn't prove anything.
My intuition was this ought to be undefined behaviour but I can't find anything to confirm this and I'm surprised that none of the GCC versions or Comeau so much as warned about it.
It looks to me like it's defined behavior. In particular, §9.1/2 says:
The standard distinguishes between using
class
,struct
orunion
when defining a class, but here, talking about a declaration, no such distinction is made -- using oneclass-key
is equivalent to any other.In C++, a struct is a class. Specifically:
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.MSVC10 throws a warning, and the warning page states that the type given in the definition will be used.
http://msdn.microsoft.com/en-us/library/695x5bes.aspx
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
TU 2
then, at least some compilers (notably MSVC++) will mangle the name of
f
differently in each translation unit, so the definition off
in TU 1 does not satisfy the reference tof
in TU 2 and you get a link error. This comes up in real life when you have a headerA.h
that defines classA
and needs to refer to classesB
,C
, andD
but a forward declaration of them suffices (so it, quite sensibly, does not includeB.h
etc) -- you better use the same keyword for those forward declarations that the actual definitions do!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."
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).