Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring a class with struct keyword and vice versa

But of course we shouldn't even think of doing such things, I know, but still this is quite interesting:

class A; //declaration
struct A {...}; //definition

struct B; //declaration
class B {...}; //definition

When I think about it, I don't see any problems if such a thing were really allowed(because struct and class are essentially the same thing). But is it (standard-wise)?

MSVC accepts and compiles it, with a warning.

like image 360
Armen Tsirunyan Avatar asked Dec 13 '22 11:12

Armen Tsirunyan


1 Answers

It is allowed according to the standard, but as some compilers warn about it, it is not very useful.

I believe the warning is/was caused by MSVC using a different name mangling for structs and classes, which would make it even less useful...


On request from @Armen:

7.1.5.3 Elaborated type specifiers, p3

... in any elaborated-type-specifier, the enum keyword shall be used to refer to an enumeration (7.2), the union class-key shall be used to refer to a union (clause 9), and 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.

like image 140
Bo Persson Avatar answered Feb 04 '23 19:02

Bo Persson