Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class T,class U> struct is_assignable; | (1) | (since C++11) |
template<class T,class U> struct is_trivially_assignable; | (2) | (since C++11) |
template<class T,class U> struct is_nothrow_assignable; | (3) | (since C++11) |
IfT
orU
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 U> constexprbool is_assignable_v= is_assignable<T, U>::value; | (since C++17) | |
template<class T,class U> constexprbool is_trivially_assignable_v= is_trivially_assignable<T, U>::value; | (since C++17) | |
template<class T,class U> constexprbool is_nothrow_assignable_v= is_nothrow_assignable<T, U>::value; | (since C++17) | |
value [static] | true ifT is assignable fromU ,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> |
This trait does not check anything outside the immediate context of the assignment expression: if the use ofT
orU
would trigger template specializations, generation of implicitly-defined special member functions etc, and those have errors, the actual assignment may not compile even ifstd::is_assignable<T,U>::value compiles and evaluates totrue.
#include <iostream>#include <string>#include <type_traits> struct Ex1{int n;}; int main(){std::cout<<std::boolalpha<<"int is assignable from int? "<< std::is_assignable<int,int>::value<<'\n'// 1 = 1; wouldn't compile<<"int& is assignable from int? "<< std::is_assignable<int&,int>::value<<'\n'// int a; a = 1; works<<"int is assignable from double? "<< std::is_assignable<int,double>::value<<'\n'<<"int& is nothrow assignable from double? "<< std::is_nothrow_assignable<int&,double>::value<<'\n'<<"string is assignable from double? "<< std::is_assignable<std::string,double>::value<<'\n'<<"Ex1& is trivially assignable from const Ex1&? "<< std::is_trivially_assignable<Ex1&,const Ex1&>::value<<'\n';}
Output:
int is assignable from int? falseint& is assignable from int? trueint is assignable from double? falseint& is nothrow assignable from double? truestring is assignable from double? trueEx1& is trivially assignable from const Ex1&? true
(C++11)(C++11)(C++11) | checks if a type has a copy assignment operator (class template)[edit] |
(C++11)(C++11)(C++11) | checks if a type has a move assignment operator (class template)[edit] |
(C++20) | specifies that a type is assignable from another type (concept)[edit] |