From cppreference.com
Defined in header
template<classT>structis_default_constructible; (1) (since C++11)template<classT>structis_trivially_default_constructible; (2) (since C++11)template<classT>structis_nothrow_default_constructible; (3) (since C++11)1) Provides the member constant value equal to std::is_constructible<T>::value.
2) Provides the member constant value equal to std::is_trivially_constructible<T>::value.
3) Provides the member constant value equal to std::is_nothrow_constructible<T>::value.
If T is not a complete type, (possibly cv-qualified) void, or an array of unknown bound, the behavior is undefined.
If an instantiation of a template above depends, directly or indirectly, on an incomplete type, and that instantiation could yield a different result if that type were hypothetically completed, the behavior is undefined.
If the program adds specializations for any of the templates described on this page, the behavior is undefined.
Helper variable templates
template<classT>inlineconstexprboolis_default_constructible_v=is_default_constructible<T>::value;(since C++17)template<classT>inlineconstexprboolis_trivially_default_constructible_v=is_trivially_default_constructible<T>::value;(since C++17)template<classT>inlineconstexprboolis_nothrow_default_constructible_v=is_nothrow_default_constructible<T>::value;(since C++17)Inherited from
Member constants
value
[static]
true if T is default-constructible, false otherwise
(public static member constant)Member functions
operator bool
converts the object to bool, returns value
(public member function)operator()
(C++14)
returns value
(public member function)Member types
Type Definition value_typebooltypestd::integral_constant<bool,value>Possible implementation
template<classT>structis_default_constructible:std::is_constructible<T>{};template<classT>structis_trivially_default_constructible:std::is_trivially_constructible<T>{};template<classT>structis_nothrow_default_constructible:std::is_nothrow_constructible<T>{};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:
,
.
std::is_default_constructible<T> does not test that Tx; would compile; it attempts
with an empty argument list (see
). Thus, std::is_default_constructible_v<constint> and std::is_default_constructible_v<constint[10]> are true.
Example
Run this code
#include<string>#include<type_traits>structS1{std::stringstr;// 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);structS2{intn;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);intmain(){}See also