std::is_same - cppreference.com
提供: cppreference.com
<tbody> </tbody>
|
|
(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>
|
|
(C++17以上) | |
std::integral_constant から継承
メンバ定数
T と U が同じ型ならば true、そうでなければ false (パブリック静的メンバ定数) |
メンバ関数
オブジェクトを bool に変換します。 value を返します (パブリックメンバ関数) | |
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 <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