Namespaces
Variants
Views
Actions

std::is_same

From cppreference.com
< cpp‎ | types
 
 
 
Type support
Basic types
Fundamental types
Fixed width integer types (C++11)
Numeric limits
C numeric limits interface
Runtime type information
Type traits
Type categories
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Constant evaluation context
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)
(C++11)
(C++17)
(C++11)(until C++20)(C++17)
 
Defined in header <type_traits>
template< class T, class U >
struct is_same;
(since C++11)

If T and U name the same type (including const/volatile qualifications), provides the member constant value equal to true. Otherwise value is false.

Commutativity is satisfied, i.e. for any two types T and U, is_same<T, U>::value == true if and only if is_same<U, T>::value == true.

Contents

[edit] Helper variable template

template< class T, class U >
inline constexpr bool is_same_v = is_same<T, U>::value;
(since C++17)

Inherited from std::integral_constant

Member constants

value
[static]
true if T and U is the same type , false otherwise
(public static member constant)

Member functions

operator bool
converts the object to bool, returns value
(public member function)
operator()
(C++14)
returns value
(public member function)

Member types

Type Definition
value_type bool
type std::integral_constant<bool, value>

[edit] Possible implementation

template<class T, class U>
struct is_same : std::false_type {};
 
template<class T>
struct is_same<T, T> : std::true_type {};

[edit] Example

#include <iostream>
#include <type_traits>
#include <cstdint>
 
void print_separator()
{
    std::cout << "-----\n";
}
 
int main()
{
    std::cout << std::boolalpha;
 
    // some implementation-defined facts
    std::cout << std::is_same<int, std::int32_t>::value << '\n';
    // usually true if 'int' is 32 bit
    std::cout << std::is_same<int, std::int64_t>::value << '\n';
    // possibly true if ILP64 data model is used
 
    print_separator();
 
    // 'float' is never an integral type
    std::cout << std::is_same<float, std::int32_t>::value << '\n'; // false
 
    print_separator();
 
    // 'int' is implicitly '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();
 
    // unlike other types, 'char' is neither 'unsigned' nor '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
}

Possible output:

true
false
-----
false
-----
true
false
true
-----
true
false
false

[edit] See also

checks if a type is an integral type
(class template) [edit]
makes the given integral type signed
(class template) [edit]
makes the given integral type unsigned
(class template) [edit]