Namespaces
Variants

std:: is_same

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
定義先ヘッダ <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) 式またはエンティティの型を取得する