Where should I put [[gnu :: transparent_union]]?

Clang supports the use of the C ++ 11 generic attribute syntax for vendor-specific attributes . This means that in theory I can use any attribute supported by Clang and mark it with syntax [[attribute]]

.

However, I am having problems using the attribute transparent_union

. I remember that C ++ allows you to put attributes basically anywhere, so I tried all of the following, to no avail:

[[gnu::transparent_union]] union x { int foo; };
// error: an attribute list cannot appear here

union [[gnu::transparent_union]] x { int foo; };
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]

union x [[gnu::transparent_union]] { int foo; };
// error: an attribute list cannot appear here
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]

union x { int foo; } [[gnu::transparent_union]];
// error: an attribute list cannot appear here
// warning: transparent_union attribute can only be applied to a union definition;
// attribute ignored [-Wignored-attributes]

      

The correct place to use the syntax __attribute__

seems to be at the end of the union definition:

union x { int foo; } __attribute__((transparent_union));
// OK

      

Where is the right place, if any, to put [[gnu::transparent_union]]

with Clang?

+3


source to share


1 answer


In the C ++ 11 standard, it shows in the grammar that after the class key comes the seq-specifier, which is union

.

class-head:
       class-key attribute-specifier-seqopt class-head-name class-virt-specifieropt base-clauseopt
       class-key attribute-specifier-seqopt base-clauseopt

[..]

class-key:
       class
       struct
       union

This is confirmed as:



union [[gnu::transparent_union]] x { int foo; };

      

seems to be the only syntax that doesn't trigger diagnostics. This is probably a Clang bug.

Keep in mind that this __attribute__

is a GNU extension and [[attribute]]

is part of C ++.

+2


source







All Articles