My internal sanity check failed so I'm rerunning it on Stackoverflow.
The following code:
#include <iostream>
#include <typeinfo>
#include <utility>
int main()
{
constexpr auto pair_of_ints = std::make_pair(1, 2);
std::cerr << typeid(pair_of_ints).name();
//static_assert(std::is_same<decltype(pair_of_ints), std::pair<int, int>>::value, "WTF");
}
produces the mangled symbol name for std::__1::pair<int, int>
on my system (XCode Clang 8.x).
If I then enable the static_assert
, it fails. I have no idea why.
How can I make this work? I have a function that returns a pair or tuple depending on the arguments passed to it and would like to verify it actually returns a pair or tuple in the correct cases.
You declared pair_of_ints
as constexpr
which implies const
:
[dcl.constexpr]#9
A
constexpr
specifier used in an object declaration declares the object asconst
.
So the type of pair_of_ints
is actually:
const std::pair<int, int>
typeid
ignores cv-qualifiers, which is why this information does not appear in the name:
[expr.typeid]#5
If the type of the expression or type-id is a cv-qualified type, the result of the
typeid
expression refers to astd::type_info
object representing the cv-unqualified type.
You could either test against the const-qualified type, or drop the const-qualifier using std::remove_const_t:
static_assert(std::is_same<decltype(pair_of_ints),
const std::pair<int, int>>::value);
static_assert(std::is_same<std::remove_const_t<decltype(pair_of_ints)>,
std::pair<int, int>>::value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With