I have been learning about trivial and standard layout types. I think I understand the basics behind it, but there is still something I am missing. Please have a look at the two following examples:
Example 1:
int main()
{
struct SomeType
{
SomeType() = default;
int x;
};
std::cout << "is_trivially_copyable: " << std::is_trivially_copyable<SomeType>::value << "\n";
std::cout << "is_trivial: " << std::is_trivial<SomeType>::value << "\n";
std::cout << "is_standard_layout: " << std::is_standard_layout<SomeType>::value << "\n";
static constexpr SomeType someType{};
return 0;
}
SomeType is trivial and I am able to static initialized it "static constexpr SomeType someType{};
".
Example 2:
int main()
{
struct SomeType
{
SomeType() {};
int x;
};
std::cout << "is_trivially_copyable: " << std::is_trivially_copyable<SomeType>::value << "\n";
std::cout << "is_trivial: " << std::is_trivial<SomeType>::value << "\n";
std::cout << "is_standard_layout: " << std::is_standard_layout<SomeType>::value << "\n";
static constexpr SomeType someType{};
return 0;
}
SomeType is not trivial, but is standard layout, line "static constexpr SomeType someType{};
" fails with an error "Error C2127 'someType': illegal initialization of 'constexpr' entity with a non-constant expression ConsoleApplication2" on MSVC compiler. If I make constructor constexpr and initialize x in constructor, this will work so my question is the following.
If I understood it right, trivial types can be static initialized, but what about non-trivial types? Let me maybe rephrase it for easier understanding, how to define a type so it can be statically initialized?