GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::is_same(3) C++ Standard Libary std::is_same(3)

std::is_same - std::is_same


Defined in header <type_traits>
template< class T, class U > (since C++11)
struct is_same;


If T and U name the same type (taking into account 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.


The behavior of a program that adds specializations for is_same
or is_same_v
(since C++17) is undefined.


Helper variable template


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

Inherited from std::integral_constant


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


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


Type Definition
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 {};

// Run this code


#include <iostream>
#include <type_traits>
#include <cstdint>


void print_separator()
{
std::cout << "-----\n";
}


int main()
{
std::cout << std::boolalpha;


// some implementation-defined facts


// usually true if 'int' is 32 bit
std::cout << std::is_same<int, std::int32_t>::value << ' '; // ~ true
// possibly true if ILP64 data model is used
std::cout << std::is_same<int, std::int64_t>::value << ' '; // ~ false


// same tests as above, except using C++17's `std::is_same_v<T, U>` format
std::cout << std::is_same_v<int, std::int32_t> << ' '; // ~ true
std::cout << std::is_same_v<int, std::int64_t> << '\n'; // ~ false


print_separator();


// compare the types of a couple variables
long double num1 = 1.0;
long double num2 = 2.0;
std::cout << std::is_same_v<decltype(num1), decltype(num2)> << '\n'; // true


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 << ' '; // true
std::cout << std::is_same<int, unsigned int>::value << ' '; // 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 << ' '; // true
std::cout << std::is_same<char, unsigned char>::value << ' '; // false
std::cout << std::is_same<char, signed char>::value << '\n'; // false


// const-qualified type T is not same as non-const T
static_assert( not std::is_same<const int, int>() );
}


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


same_as specifies that a type is the same as another type
(C++20) (concept)
decltype specifier(C++11) obtains the type of an expression or an entity

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.