◐ Shell
clean mode source ↗

std::is_same - cppreference.com

提供: cppreference.com

<tbody> </tbody>

template< class T, class U > struct is_same;

(C++11以上)

TU が同じ型 (const/volatile 修飾も考慮します) であれば、 true に等しいメンバ定数 value が提供されます。 そうでなければ valuefalse です。

交換法則が満たされます。 すなわち、任意の2つの型 TU について、 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 から継承

メンバ定数

TU が同じ型ならば 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

関連項目