std:: is_same
| Type traits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time rational arithmetic | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Compile-time integer sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++14)
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定義先ヘッダ
<type_traits>
|
||
|
template
<
class
T,
class
U
>
struct is_same ; |
(C++11以降) | |
T
と
U
が(const/volatile修飾を考慮して)同じ型を指す場合、メンバ定数
value
を
true
に設定します。それ以外の場合、
value
は
false
となります。
可換性が満たされる。すなわち、任意の2つの型
T
と
U
について、
is_same
<
T, U
>
::
value
==
true
であることと
is_same
<
U, T
>
::
value
==
true
であることは同値である。
プログラムが
std::is_same
または
std::is_same_v
(C++17以降)
に対する特殊化を追加する場合、動作は未定義です。
目次 |
ヘルパー変数テンプレート
|
template
<
class
T,
class
U
>
constexpr bool is_same_v = is_same < T, U > :: value ; |
(C++17以降) | |
std::integral_constant から継承
メンバ定数
|
value
[static]
|
true
T
と
U
が同じ型の場合、
false
それ以外の場合
(公開静的メンバ定数) |
メンバ関数
|
operator bool
|
オブジェクトを
bool
に変換し、
value
を返す
(公開メンバ関数) |
|
operator()
(C++14)
|
value
を返す
(公開メンバ関数) |
メンバ型
| 型 | 定義 |
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; // 実装定義の事実 // 'int'が32ビットの場合に通常true SHOW( std::is_same<int, std::int32_t>::value ); // 場合によってはtrue // ILP64データモデル使用時に可能性あり SHOW( std::is_same<int, std::int64_t>::value ); // 場合によってはfalse // 上記と同じテスト、C++17のstd::is_same_v<T, U>形式を使用 SHOW( std::is_same_v<int, std::int32_t> ); // 場合によってはtrue SHOW( std::is_same_v<int, std::int64_t> ); // 場合によってはfalse // いくつかの変数の型を比較 long double num1 = 1.0; long double num2 = 2.0; static_assert( std::is_same_v<decltype(num1), decltype(num2)> == true ); // 'float'は決して整数型ではない static_assert( std::is_same<float, std::int32_t>::value == false ); // 'int'は暗黙的に'signed' static_assert( std::is_same_v<int, int> == true ); static_assert( std::is_same_v<int, unsigned int> == false ); static_assert( std::is_same_v<int, signed int> == true ); // 他の型とは異なり、'char'は'unsigned'でも'signed'でもない static_assert( std::is_same_v<char, char> == true ); static_assert( std::is_same_v<char, unsigned char> == false ); static_assert( std::is_same_v<char, signed char> == false ); // const修飾された型Tは非const Tと同じではない static_assert( !std::is_same<const int, int>() ); } #undef SHOW
出力例:
std::is_same<int, std::int32_t>::value : true std::is_same<int, std::int64_t>::value : false std::is_same_v<int, std::int32_t> : true std::is_same_v<int, std::int64_t> : false
関連項目
|
(C++20)
|
型が別の型と同じであることを指定する
(コンセプト) |
decltype
指定子
(C++11)
|
式またはエンティティの型を取得する |