std::is_constructible,std::is_trivially_constructible,std::is_nothrow_constructible (3) - Linux Manuals

std::is_constructible,std::is_trivially_constructible,std::is_nothrow_constructible: std::is_constructible,std::is_trivially_constructible,std::is_nothrow_constructible


std::is_constructible,std::is_trivially_constructible,std::is_nothrow_constructible - std::is_constructible,std::is_trivially_constructible,std::is_nothrow_constructible


Defined in header <type_traits>
template< class T, class... Args > (1) (since C++11)
struct is_constructible;
template< class T, class... Args > (2) (since C++11)
struct is_trivially_constructible;
template< class T, class... Args > (3) (since C++11)
struct is_nothrow_constructible;

1) If T is an object or reference type and the variable definition T obj(std::declval<Args>()...); is well-formed, provides the member constant value equal to true. In all other cases, value is false.
For the purposes of this check, the variable definition is never interpreted as a function declaration, and the use of std::declval is not considered an odr-use. Access_checks are performed as if from a context unrelated to T and any of the types in Args. Only the validity of the immediate context of the variable definition is considered.
2) same as 1), but the variable definition does not call any operation that is not trivial. For the purposes of this check, the call to std::declval is considered trivial.
3) same as 1), but the variable definition is noexcept.
T and all types in the parameter pack Args shall each be a complete type, (possibly cv-qualified) void, or an array of unknown bound. Otherwise, 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.

Helper variable templates

template< class T, class... Args > (since C++17)
inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value;
template< class T, class... Args > (since C++17)
inline constexpr bool is_trivially_constructible_v = is_trivially_constructible<T, Args...>::value;
template< class T, class... Args > (since C++17)
inline constexpr bool is_nothrow_constructible_v = is_nothrow_constructible<T, Args...>::value;

Inherited from std::integral_constant

Member constants

value true if T is constructible from Args... , false otherwise
         (public static member constant)

Member functions

              converts the object to bool, returns value
operator bool (public member function)

operator() returns value
              (public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>


In many implementations, is_nothrow_constructible also checks if the destructor throws because it is effectively noexcept(T(arg)). Same applies to is_trivially_constructible, which, in these implementations, also requires that the destructor is trivial: GCC_bug_51452 LWG_issue_2116.


// Run this code

  #include <iostream>
  #include <type_traits>

  class Foo {
      int v1;
      double v2;
      Foo(int n) : v1(n), v2() {}
      Foo(int n, double f) noexcept : v1(n), v2(f) {}

  int main() {
      std::cout << "Foo is ...\n" << std::boolalpha
                << "\tTrivially-constructible from const Foo&? "
                << std::is_trivially_constructible<Foo, const Foo&>::value << '\n'
                << "\tTrivially-constructible from int? "
                << std::is_trivially_constructible<Foo, int>::value << '\n'
                << "\tConstructible from int? "
                << std::is_constructible<Foo, int>::value << '\n'
                << "\tNothrow-constructible from int? "
                << std::is_nothrow_constructible<Foo, int>::value << '\n'
                << "\tNothrow-constructible from int and double? "
                << std::is_nothrow_constructible<Foo, int, double>::value << '\n';


  Foo is ...
          Trivially-constructible from const Foo&? true
          Trivially-constructible from int? false
          Constructible from int? true
          Nothrow-constructible from int? false
          Nothrow-constructible from int and double? true

See also

is_nothrow_default_constructible checks if a type has a default constructor
                                   (class template)

is_nothrow_copy_constructible checks if a type has a copy constructor
                                   (class template)

is_nothrow_move_constructible checks if a type can be constructed from an rvalue reference
                                   (class template)