C11 Anonymous structs

287 views
Skip to first unread message

Matthew Fioravante

unread,
Sep 30, 2018, 5:34:55 PM9/30/18
to ISO C++ Standard - Future Proposals
Is there any good reason why the standard technically doesn't support anonymous structs like:

struct vec {
 
union {
   
struct {  
     
double x, y, z;
   
};
   
double v[3];
 
};
};

Compiling this with gcc -pedantic results in a warning:

warning: ISO C++ prohibits anonymous structs

When I google about this, people complain about maybe it invokes UB. But just giving the anonymous struct a name is equivalent and allowed:


struct vec {
 
union {
   
struct {  
     
double x, y, z;
   
} s;
   
double v[3];
 
};
};

Also it's a C11 feature so we should be compatible.

I'm guess it's just another one nobody took time to write a proposal for?

Jens Maurer

unread,
Oct 1, 2018, 1:48:01 AM10/1/18
to std-pr...@isocpp.org
On 09/30/2018 11:34 PM, Matthew Fioravante wrote:
> Is there any good reason why the standard technically doesn't support anonymous structs like:
>
> ||
> structvec {
> union{
> struct{ </>
> doublex,y,z;
> };
> doublev[3];
> };
> };
>
> Compiling this with gcc -pedantic results in a warning:
>
> |warning: ISO C++ prohibits anonymous structs|
>
> When I google about this, people complain about maybe it invokes UB. But just giving the anonymous struct a name is equivalent and allowed:
>
>
> ||
> structvec {
> union{
> struct{
> doublex,y,z;
> }s;
> doublev[3];
> };
> };
>
> Also it's a C11 feature so we should be compatible.
>
> I'm guess it's just another one nobody took time to write a proposal for?

My initial guess is, yes.

Note, however, that the core language wording for anonymous unions has been
the source of some headaches over the past decades; it would be good not to
repeat that experience with anonymous non-union classes.

Jesn

Peter Sommerlad

unread,
Oct 1, 2018, 3:14:07 AM10/1/18
to std-pr...@isocpp.org
The first version of your code declares an unnamed type only and no member. Since there is no valid way to use that type in C++ it is IMHO correct to not allow that. Even in the second version please note that the type punning that those unions are often used for in C is not valid in the C++ type system, except for very specific cases. 

Regards
Peter

sent from a mobile device.
Prof. Peter Sommerlad
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/8dadb4a9-7487-4328-a79d-3e289e9ec613%40isocpp.org.

Ray Hamel

unread,
Oct 1, 2018, 8:16:13 PM10/1/18
to ISO C++ Standard - Future Proposals
Peter,

the type punning that those unions are often used for in C is not valid in the C++ type system, except for very specific cases.

I believe it is now well-defined (for standard-layout types) as of C++17.

- Ray

Matthew Fioravante

unread,
Oct 2, 2018, 10:06:59 AM10/2/18
to ISO C++ Standard - Future Proposals
Standardeze technicalities aside, there is no reason my example shouldn't work, especially since compilers already need to support it for C11.

Maybe this one can be added as a defect report? How does one report a defect?

Nicolas Lesser

unread,
Oct 2, 2018, 1:22:47 PM10/2/18
to std-pr...@isocpp.org
On Tue, Oct 2, 2018 at 4:07 PM Matthew Fioravante <fmatth...@gmail.com> wrote:
Standardeze technicalities aside, there is no reason my example shouldn't work, especially since compilers already need to support it for C11.
 
"why not?" is not a good motivation to add something to the standard. You need to provide actual motivation why this feature is useful and why we can't achieve this today already.


Maybe this one can be added as a defect report? How does one report a defect?

You email Mike Miller, but this isn't a defect, it's a new feature. Defect reports are for things like (unintentional) oversights, unclear wording, wording holes, ... 

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Message has been deleted

floria...@gmail.com

unread,
Oct 4, 2018, 7:52:47 AM10/4/18
to ISO C++ Standard - Future Proposals
C and C++ are two different and now diverging languages.

C has no notion of nested structs so when you write this:
struct Foo {
 
struct Bar {
   
int i;
 
};
};
You are defining an empty struct Foo and the definition of Bar is just discarded.
While in C++, you also define Foo::Bar.

What I want to highlight: the behavior of the two languages is already different without considering anonymous structs. So just because C does it is not a valid reason to put it in C++.

That being said, I think it is still a valuable feature for C++ on its own.

Tom Honermann

unread,
Oct 4, 2018, 9:33:45 AM10/4/18
to std-pr...@isocpp.org, floria...@gmail.com
On 10/04/2018 07:52 AM, floria...@gmail.com wrote:
C and C++ are two different and now diverging languages.

C has no notion of nested structs so when you write this:
struct Foo {
 
struct Bar {
   
int i;
 
};
};
You are defining an empty struct Foo and the definition of Bar is just discarded.

Bar isn't actually discarded; it is still a defined class at file scope in C:


struct Foo {
  struct Bar {
    int i;
  };
};
struct Bar b = { 1 }; // Ok in C, ill-formed in C++ (Bar is an incomplete type)

Tom.

While in C++, you also define Foo::Bar.

What I want to highlight: the behavior of the two languages is already different without considering anonymous structs. So just because C does it is not a valid reason to put it in C++.

That being said, I think it is still a valuable feature for C++ on its own.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.

floria...@gmail.com

unread,
Oct 4, 2018, 9:40:08 AM10/4/18
to ISO C++ Standard - Future Proposals


Le jeudi 4 octobre 2018 15:33:45 UTC+2, Tom Honermann a écrit :
On 10/04/2018 07:52 AM, floria...@gmail.com wrote:
C and C++ are two different and now diverging languages.

C has no notion of nested structs so when you write this:
struct Foo {
 
struct Bar {
   
int i;
 
};
};
You are defining an empty struct Foo and the definition of Bar is just discarded.

Bar isn't actually discarded; it is still a defined class at file scope in C:

struct Foo {
  struct Bar {
    int i;
  };
};
struct Bar b = { 1 }; // Ok in C, ill-formed in C++ (Bar is an incomplete type)


I was mislead by GCC: warning: declaration does not declare anything

But that means the difference is even bigger than what I expected, and that makes the argument "it's already in C11" even less relevant.

Victor Dyachenko

unread,
Oct 5, 2018, 2:11:10 AM10/5/18
to ISO C++ Standard - Future Proposals, floria...@gmail.com
On Thursday, October 4, 2018 at 4:40:08 PM UTC+3, floria...@gmail.com wrote:

But that means the difference is even bigger than what I expected, and that makes the argument "it's already in C11" even less relevant.

I dislike this approach. For each incompatibility between C and C++ we should have an explanation "why can't we afford this feature in C++". We have such explanations for implicit pointers conversion, nested structs, sizeof('a'), complex types, VLAs, restrict, etc. If we don't have such clear explanation, we should just eliminate the incompatibility.

floria...@gmail.com

unread,
Oct 5, 2018, 4:57:53 AM10/5/18
to ISO C++ Standard - Future Proposals
I have the impression you misunderstood my point, so let me rephrase.

Anonymous structs is based on "nested" structs (at least syntax-wise). But nested structs is supported (somehow) by the 2 languages with 2 different meaning:
in C, the nested definition just defines the struct in the global namespace, whereas in C++, it is in the outer class namespace.
Those are incompatible and never will be (because C has only the global namespace).
The anonymous struct is a "variant" of this syntax.
All that to say: what applies to C doesn't apply to C++ because of this incompatibility that can never be lifted.

Don't get me wrong: I never said we shouldn't have anonymous struct in C++. I'm just saying: because it is close to a "feature" that is essentially incompatible between the 2 languages, "it is in C, so we need it C++" is not a valid argument on its own.
It can only be a small plus in the end once we have real arguments for anonymous structs in C++.

My whole point is: we need to have real reasons to put it in C++, and just say "it's already in C" is not enough.
I'm all in favor to have anonymous structs in C++, but I haven't thought long enough yet to raise a real argument for it.

And please remember the question of the OP: Is there any good reason why the standard technically doesn't support anonymous structs?
The answer is yes: nobody has expressed enough interest to make it part of C++, and just "because it's in C" is far from enough.
Reply all
Reply to author
Forward
0 new messages