std::is_trivial_default_constructible<T>, what's required?!

Hello Clang/C++ experts!

I’m confused on what it takes to be std::is_trivial_default_constructible?

This doesn’t even compile (leaving x_ uninitialized compiles but I don’t see why it’s not trivial to initialize a POD member inline):

class Test {
public:
int x_ = 0;
};

static_assert(std::is_trivially_default_constructible::value, “”);

→ error: static_assert failed due to requirement ‘std::is_trivially_default_constructible::value’ “”

I also tried making a constexpr Test() = default; constructor but I can’t get anything with POD members initialized inline to pass the std::is_trivially_default_constructible test…?!

Thanks!
Gab

A default constructor is trivial if it is not user-provided and if … no non-static data member of its class has a default member initializer …

http://eel.is/c++draft/class.ctor#6.2

Ah ah! Had missed that. That’s unfortunate :frowning:

Thanks

Hello Clang/C++ experts!

I'm confused on what it takes to be std::is_trivial_default_constructible?

To be trivially default constructible means to be default constructible
*without doing anything*.

This doesn't even compile (leaving x_ uninitialized compiles but I don't
see why it's not trivial to initialize a POD member inline):

class Test {
public:
  int x_ = 0;
};

static_assert(std::is_trivially_default_constructible<Test>::value, "");

-> error: static_assert failed due to requirement
'std::is_trivially_default_constructible<Test>::value' ""

I also tried making a constexpr Test() = default; constructor but I can't
get anything with POD members initialized inline to pass the
std::is_trivially_default_constructible test...?!

If you need to initialize something, that's not trivial.

-- James

Right… What I want is way to tell if the compiler will evaluate this at compile-time (e.g. to guarantee no static initializer for globals of trivial types with only zero-initialized POD members).

`constexpr` (or the [[clang::require_constant_initialization]] attribute)
is what you want to ensure compile-time initialization. Happily it can do
a lot more than zero-initialization.

-- James

p.s. This isn't really the right list for general questions about C++.