Search code examples
c++type-traits

Which operation is not trivial here?


Compilers agree, that the below X and Y are default-constructible, but not trivially (demo).

#include <type_traits>

struct X { int x {}; };
struct Y { int y = 0; };

static_assert(std::is_default_constructible_v<X>);
static_assert(std::is_default_constructible_v<Y>);
static_assert(!std::is_trivially_default_constructible_v<X>);
static_assert(!std::is_trivially_default_constructible_v<Y>);

Why are they not trivial? According to cppreference.com (see is_trivially_constructible) a non-trivial operation must have been called during default-construction. Which one is that?


Solution

  • https://en.cppreference.com/w/cpp/language/default_constructor#Trivial_default_constructor says:

    The default constructor for class T is trivial (i.e. performs no action) if all of the following is true:

    • ...
    • T has no non-static members with default initializers. (since C++11)
    • ...