Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
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) |
T
is an object or reference type and the variable definitionT obj(std::declval<Args>()...); is well-formed, provides the member constantvalue
equal totrue. In all other cases,value
isfalse.T
and any of the types inArgs
. Only the validity of the immediate context of the variable definition is considered.noexcept
.IfT
or any type in the parameter packArgs
is not a complete type, (possibly cv-qualified)void, or an array of unknown bound, 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.
If the program adds specializations for any of the templates described on this page, the behavior is undefined.
Contents |
template<class T,class...Args> inlineconstexprbool is_constructible_v= | (since C++17) | |
template<class T,class...Args> inlineconstexprbool is_trivially_constructible_v= | (since C++17) | |
template<class T,class...Args> inlineconstexprbool is_nothrow_constructible_v= | (since C++17) | |
value [static] | true ifT is constructible fromArgs... ,false otherwise(public static member constant) |
operator bool | converts the object tobool, returnsvalue (public member function) |
operator() (C++14) | returnsvalue (public member function) |
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 effectivelynoexcept(T(arg)). Same applies tois_trivially_constructible
, which, in these implementations, also requires that the destructor is trivial:GCC bug 51452LWG issue 2116.
#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(){auto is=[](bool o){return(o?"\t""is ":"\t""isn't ");};std::cout<<"Foo ...\n"<< is(std::is_trivially_constructible_v<Foo,const Foo&>)<<"Trivially-constructible from const Foo&\n"<< is(std::is_trivially_constructible_v<Foo,int>)<<"Trivially-constructible from int\n"<< is(std::is_constructible_v<Foo,int>)<<"Constructible from int\n"<< is(std::is_nothrow_constructible_v<Foo,int>)<<"Nothrow-constructible from int\n"<< is(std::is_nothrow_constructible_v<Foo,int,double>)<<"Nothrow-constructible from int and double\n";}
Output:
Foo ... is Trivially-constructible from const Foo& isn't Trivially-constructible from int is Constructible from int isn't Nothrow-constructible from int is Nothrow-constructible from int and double
checks if a type has a default constructor (class template)[edit] | |
(C++11)(C++11)(C++11) | checks if a type has a copy constructor (class template)[edit] |
(C++11)(C++11)(C++11) | checks if a type can be constructed from an rvalue reference (class template)[edit] |
(C++20) | specifies that a variable of the type can be constructed from or bound to a set of argument types (concept)[edit] |