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

< cpp‎ | types
Revision as of 16:50, 1 July 2013 by P12bot (Talk | contribs)

Type support
Type properties
Type trait constants
Supported operations
Relationships and property queries
Type modifications
Type transformations
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 rvalue references 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.


Inherited from std::integral_constant

Member constants

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)
returns value
(public member function)

Member types

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


#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

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]