std::is_default_constructible, std::is_trivially_default_constructible, std::is_nothrow_default_constructible

Header: <type_traits>

  1. Provides the member constant value equal to std::is_constructible::value.

# Declarations

template< class T >
struct is_default_constructible;

(since C++11)

template< class T >
struct is_trivially_default_constructible;

(since C++11)

template< class T >
struct is_nothrow_default_constructible;

(since C++11)

# Notes

In many implementations, std::is_nothrow_default_constructible also checks if the destructor throws because it is effectively noexcept(T()). Same applies to std::is_trivially_default_constructible, which, in these implementations, also requires that the destructor is trivial: GCC bug 51452, LWG issue 2116.

std::is_default_constructible does not test that T x; would compile; it attempts direct-initialization with an empty argument list (see std::is_constructible). Thus, std::is_default_constructible_v and std::is_default_constructible_v<const int[10]> are true.

# Example

#include <string>
#include <type_traits>
 
struct S1
{
    std::string str; // member has a non-trivial default constructor
};
static_assert(std::is_default_constructible_v<S1> == true);
static_assert(std::is_trivially_default_constructible_v<S1> == false);
 
struct S2
{
    int n;
    S2() = default; // trivial and non-throwing
};
static_assert(std::is_trivially_default_constructible_v<S2> == true);
static_assert(std::is_nothrow_default_constructible_v<S2> == true);
 
int main() {}

# See also