Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Type properties
(C++11)
(C++11)
(C++11)
(C++14)
Type trait constants
Metafunctions
(C++17)
Supported operations
is_constructibleis_trivially_constructibleis_nothrow_constructible
(C++11)(C++11)(C++11)
Relationships and property queries
(C++11)
(C++11)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)
 
Defined in header <type_traits>
template< class T, class... Args >
struct is_constructible;
(1) (since C++11)
template< class T, class... Args >
struct is_trivially_constructible;
(2) (since C++11)
template< class T, class... Args >
struct is_nothrow_constructible;
(3) (since C++11)

1) If T is a function type, provides the member constant value equal to false. Otherwise, if the variable definition T obj(std::declval<Args>()...); is well-formed, value is equal to true, else 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.

Contents

[edit] Helper variable templates

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

Inherited from std::integral_constant

Member constants

value
[static]
true if T is constructible from Args... , 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_type bool
type std::integral_constant<bool, value>

[edit] Notes

In many implementations, is_nothrow_constructible also checks if the destructor throws because it is effectively noexept(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.

[edit] Example

#include <iostream>
#include <type_traits>
 
class Foo {
    int v1;
    double v2;
 public:
    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';
}

Output:

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

[edit] See also

checks if a type has a default constructor
(class template) [edit]
checks if a type has a copy constructor
(class template) [edit]
checks if a type has a move constructor
(class template) [edit]
checks if the specified type supports uses-allocator construction
(class template) [edit]
variable template alias of std::is_constructible::value
(variable template) [edit]
variable template alias of std::is_trivially_constructible::value
(variable template) [edit]
variable template alias of std::is_nothrow_constructible::value
(variable template) [edit]