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

From Cppreference

Jump to: navigation, search
Defined in header <type_traits>

template< class T, class... Args >
struct is_constructible;
(1) (C++11 feature)
template< class T, class... Args >
struct is_trivially_constructible;
(2) (C++11 feature)
template< class T, class... Args >
struct is_nothrow_constructible;
(3) (C++11 feature)

1) If the expression T obj(arg1, arg2, ... argN); is well-formed, given rvalue referneces to Args... as arguments, provides the member constant value equal true. For any other type, value is false.

2) same as 1), but the constructor expression does not call any operation that is not trivial.

3) same as 1), but the constructor expression is noexcept.

Contents

Inherited from std::integral_constant

Member constants

value 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)

Member types

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

[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

is_default_constructible
is_trivially_default_constructible
is_nothrow_default_constructible
(C++11)
(C++11)
(C++11)
checks if a type has a default constructor
(class template)
is_copy_constructible
is_trivially_copy_constructible
is_nothrow_copy_constructible
(C++11)
(C++11)
(C++11)
checks if a type has a copy constructor
(class template)
is_move_constructible
is_trivially_move_constructible
is_nothrow_move_constructible
(C++11)
(C++11)
(C++11)
checks if a type has a move constructor
(class template)
uses_allocator (C++11)
checks if the specified type supports uses-allocator construction
(class template)