std::meta::is_same_type
From cppreference.com
| Defined in header <meta>
|
||
consteval bool is_same_type( std::meta::info r1, std::meta::info r2 );
|
(since C++26) | |
If two reflections r1 and r2 represent the same type (taking into account cv-qualifications), returns true. Otherwise, returns false.
Commutativity is satisfied, i.e., for any two reflections:
is_same_type(r1, r2) == true if and only if
is_same_type(r2, r1) == true.
Parameters
| r1, r2 | - | reflections to compare |
Return value
true if r1 and r2 represent the same type, false otherwise.
Exceptions
Throws std::meta::exception if either r1 or r2 does not represent a type or type alias.
Notes
If r1 is a reflection of the type int and r2 is a reflection of an alias to the type int, then r1 == r2 is false but std::meta::is_same_type(r1, r2) is true. Also, r1 == std::meta::dealias(r2) is true.
Example
Run this code
#include <cstdint>
#include <meta>
#include <print>
constexpr long double num1{1.0};
constexpr long double num2{2.0};
static_assert
(""
// compare the types of a couple variables
&& is_same_type(^^decltype(num1), ^^decltype(num2))
// ‘float’ is never an integral type
&& !is_same_type(^^float, ^^int32_t)
// ‘int’ is implicitly ‘signed’
&& is_same_type(^^int, ^^int)
&& !is_same_type(^^int, ^^unsigned int)
&& is_same_type(^^int, ^^signed int)
// unlike other types, ‘char’ is neither ‘unsigned’ nor ‘signed’
&& is_same_type(^^char, ^^char)
&& !is_same_type(^^char, ^^unsigned char)
&& !is_same_type(^^char, ^^signed char)
// const-qualified type T is not same as non-const T
&& !is_same_type(^^const int, ^^int)
);
#define SHOW(...) std::println("{} == {}", #__VA_ARGS__, __VA_ARGS__)
int main()
{
// Some implementation-defined facts:
// usually true if ‘int’ is 32 bit
SHOW(is_same_type(^^int, std::meta::dealias(^^int32_t))); // maybe true
// possibly true if ILP64 data model is used
SHOW(is_same_type(^^int, std::meta::dealias(^^int64_t))); // maybe false
}
Possible output:
is_same_type(^^int, std::meta::dealias(^^int32_t)) == true
is_same_type(^^int, std::meta::dealias(^^int64_t)) == false
See also
decltype specifier(C++11)
|
obtains the type of an expression or an entity |
(C++26) |
checks if reflection represents a type (function) |
(C++20) |
specifies that a type is the same as another type (concept) |
(C++11) |
checks if two types are the same (class template) |