Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which standards allow anonymous structs and unions in C and C++?

Where can we use anonymous structs and unions?

struct
{
    int bar;
}; // anonymous struct

union
{
    int bar;
}; // anonymous union

I think that we can do it in the following standards:

  • unions - C++98, C++03, C++11, C11

  • structs - C11

Am i right or not

like image 500
FrozenHeart Avatar asked Sep 10 '12 15:09

FrozenHeart


Video Answer


1 Answers

The statement about C is correct: the standardization of anonymous structs and unions is pretty new (C11) cfr. GCC man.

Note that your preferred compiler could enable those features as extensions to the current supported standard (e.g. GNU C99 extensions).

Then, checking old specs, it seems that anonymous unions are supported in C++ since 1998.

It is common knowledge that anonymous structs are forbidden in C++ and I did not find any amendment. As of Visual studio 2012, C++ is confirmed not supporting this feature.

like image 54
ziu Avatar answered Sep 28 '22 17:09

ziu