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

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Primary type categories
(C++11)
(C++14)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++14)
(C++11)
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)
(C++11)
(C++11)
Type modifications
(C++11)(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Type transformations
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type trait constants
 
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 the expression T obj(arg1, arg2, ... argN); is well-formed, given declval<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
[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)

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

checks if a type has a default constructor
(class template)
checks if a type has a copy constructor
(class template)
checks if a type has a move constructor
(class template)
checks if the specified type supports uses-allocator construction
(class template)