名前空間
変種

std::is_same

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
型サポート
型の性質
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(C++20未満)
(C++11)(C++20で非推奨)
(C++11)
型特性定数
メタ関数
(C++17)
定数評価文脈
サポートされている操作
関係と性質の問い合わせ
型変更
(C++11)(C++11)(C++11)
型変換
(C++11)
(C++11)
(C++17)
(C++11)(C++20未満)(C++17)
 
<tbody> </tbody>
ヘッダ <type_traits> で定義
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 から継承

メンバ定数

value
[静的]
TU が同じ型ならば 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 {};

#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

関連項目

型が整数型かどうか調べます
(クラステンプレート) [edit]
指定された整数型を符号付きにします
(クラステンプレート) [edit]
指定された整数型を符号なしにします
(クラステンプレート) [edit]