gcc and msvc failed to compile this piece of code, with error msg namespace-scope anonymous aggregates must be static
. But clang has no problem compiling this.
https://godbolt.org/z/WecT6vP91
namespace {
union {
int a;
long b;
};
}
https://en.cppreference.com/w/cpp/language/union says
Namespace-scope anonymous unions must be declared static unless they appear in an unnamed namespace.
That seems a bug of gcc and msvc?
Edited:
However, Clang rejects to compile non-static anonymous union in named namespace nested in an unnamed one. Can the rule be changed to "... unless they appear in a direct or indirect unnamed namespace"(this seems have another problem, see below) or "... unless they have internal linkage"? Or why can not?
namespace {
namespace ns {
union {
int a;
long b;
};
}
}
According to External linkage for name inside unnamed namespace, names in unnamed namespace can have C language linkage. Clang even accepts non-static anonymous union that has C language linkage, without actually generating linking symbols though, which doesn't seem to make sense. Is it allowed by the standard? If it's allowed, what's the effect of extern "C"
inside direct or indirect umnamed namespace?
namespace {
extern "C" union {
int a;
long b;
};
}