mardi 30 avril 2019

c++: Is it legal to define an unnamed struct?

Is the following code legal:

struct
{
    int  x;
};

This code simply defines an unnamed structure. I do not intent to create objects of this type, nor do I need this structure in any other way. It simply appears in the source as a side effect of some complex macro expansion.

Useless though it is, I see no problem with it. Just another piece of code that can be compiled and then optimized out completely.

However, in the real world the outcome is quite different from my expectations:

gcc 8.3 reports an error:

error: abstract declarator '<unnamed struct>' used as declaration

clang 8.0.0 reports an error too:

error: anonymous structs and classes must be class members
warning: declaration does not declare anything [-Wmissing-declarations]

Only MSVC 2017 sees no problem with such source.

So, the question is: who's right? Is there a relevant quote from the Standard that explicitly forbids such declarations?

Aucun commentaire:

Enregistrer un commentaire