Consider the following:
#include <type_traits>
struct MyType {
int val;
MyType(void) = default;
MyType(int v) : val(v) {}
};
static_assert(std::is_standard_layout<MyType>::value,"Implementation error!");
static_assert(std::is_trivial<MyType>::value,"Implementation error!");
static_assert(std::is_pod<MyType>::value,"Implementation error!");
struct Wrapper {
struct {
MyType t;
};
};
MSVC, Clang, and Intel C++ all compile it fine. But g++4.9 foo.cpp -std=c++11
tells me:
14 : error: member 'MyType Wrapper::<anonymous struct>::t' with constructor not allowed in anonymous aggregate
MyType t;
^
Compilation failed
Notice that the static_assert
s ensure that MyType
is a standard layout type, a trivial type, and moreover is actually POD (note that after C++11, PODs are allowed to have constructors).
I couldn't find anything authoritative about what types are allowed inside anonymous structures. What I did find (mostly here on SO) suggest that being a POD type is sufficient. Apparently, it is not.
My question: If being a POD type is actually insufficient for being in an anonymous structure, what is sufficient? Or perhaps, since GCC is differing from all other compilers, is this a problem with GCC?