std::is_same
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <type_traits> で定義
|
||
template< class T, class U > struct is_same; |
(C++11以上) | |
T と U が同じ型 (const/volatile 修飾も考慮します) であれば、 true に等しいメンバ定数 value が提供されます。 そうでなければ value は false です。
交換法則が満たされます。 すなわち、任意の2つの型 T と U について、 is_same<U, T>::value == true の場合に限り is_same<T, U>::value == true です。
ヘルパー変数テンプレート
<tbody> </tbody> template< class T, class U > inline constexpr bool is_same_v = is_same<T, U>::value; |
(C++17以上) | |
std::integral_constant から継承
メンバ定数
value [静的] |
T と U が同じ型ならば true、そうでなければ 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 {};
|
例
Run this code
#include <iostream>
#include <type_traits>
#include <cstdint>
void print_separator()
{
std::cout << "-----\n";
}
int main()
{
std::cout << std::boolalpha;
// いくつかの処理系定義の事柄。
std::cout << std::is_same<int, std::int32_t>::value << '\n';
// 「int」が32ビットであれば通常 true です。
std::cout << std::is_same<int, std::int64_t>::value << '\n';
// ILP64 データモデルが使用されていれば true になる可能性があります。
print_separator();
// 「float」が整数型であることはありません。
std::cout << std::is_same<float, std::int32_t>::value << '\n'; // false
print_separator();
// 「int」は暗黙に「signed」です。
std::cout << std::is_same<int, int>::value << "\n"; // true
std::cout << std::is_same<int, unsigned int>::value << "\n"; // false
std::cout << std::is_same<int, signed int>::value << "\n"; // true
print_separator();
// 他の型と異なり、「char」は「unsigned」でも「signed」でもありません。
std::cout << std::is_same<char, char>::value << "\n"; // true
std::cout << std::is_same<char, unsigned char>::value << "\n"; // false
std::cout << std::is_same<char, signed char>::value << "\n"; // false
}
出力例:
true
false
-----
false
-----
true
false
true
-----
true
false
false
関連項目
(C++11) |
型が整数型かどうか調べます (クラステンプレート) |
(C++11) |
指定された整数型を符号付きにします (クラステンプレート) |
(C++11) |
指定された整数型を符号なしにします (クラステンプレート) |