Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
(C++14) |
Defined in header <type_traits> | ||
template<class T,class U> struct is_same; | (since C++11) | |
IfT
andU
name the same type (taking into account const/volatile qualifications), provides the member constantvalue equal totrue. Otherwisevalue isfalse.
Commutativity is satisfied, i.e. for any two typesT
andU
,is_same<T, U>::value==true if and only ifis_same<U, T>::value==true.
If the program adds specializations forstd::is_same
orstd::is_same_v
(since C++17), the behavior is undefined.
Contents |
template<class T,class U> constexprbool is_same_v= is_same<T, U>::value; | (since C++17) | |
value [static] | true ifT andU are the same type,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> |
template<class T,class U>struct is_same:std::false_type{}; template<class T>struct is_same<T, T>:std::true_type{}; |
#include <cstdint>#include <iostream>#include <type_traits> #define SHOW(...) std::cout << #__VA_ARGS__ << " : " << __VA_ARGS__ << '\n' int main(){std::cout<<std::boolalpha; // some implementation-defined facts // usually true if 'int' is 32 bit SHOW( std::is_same<int,std::int32_t>::value);// maybe true// possibly true if ILP64 data model is used SHOW( std::is_same<int,std::int64_t>::value);// maybe false // same tests as above, except using C++17's std::is_same_v<T, U> format SHOW( std::is_same_v<int,std::int32_t>);// maybe true SHOW( std::is_same_v<int,std::int64_t>);// maybe false // compare the types of a couple variableslongdouble num1=1.0;longdouble num2=2.0; static_assert( std::is_same_v<decltype(num1), decltype(num2)>==true); // 'float' is never an integral type static_assert( std::is_same<float,std::int32_t>::value==false); // 'int' is implicitly 'signed' static_assert( std::is_same_v<int,int>==true); static_assert( std::is_same_v<int,unsignedint>==false); static_assert( std::is_same_v<int,signedint>==true); // unlike other types, 'char' is neither 'unsigned' nor 'signed' static_assert( std::is_same_v<char,char>==true); static_assert( std::is_same_v<char,unsignedchar>==false); static_assert( std::is_same_v<char,signedchar>==false); // const-qualified type T is not same as non-const T static_assert(!std::is_same<constint,int>());}#undef SHOW
Possible output:
std::is_same<int, std::int32_t>::value : truestd::is_same<int, std::int64_t>::value : falsestd::is_same_v<int, std::int32_t> : truestd::is_same_v<int, std::int64_t> : false
(C++20) | specifies that a type is the same as another type (concept)[edit] |
decltype specifier(C++11) | obtains the type of an expression or an entity[edit] |