I have below code snippet where i declare a variable called pval
which is attempting to derive T&&
on a T*
[ with T
being int
]. As per type information [ decoded using abi ] the type derived is int*
.
But when I compare the int*
type with decltype(pval)
it returns zero rather than 1 which means it treats pval
as different type other than int*
. So which one is the wrong pval
being int*
as reported by typeid
or is_same
which indicates comparison as false.
#include<iostream>
#include<string>
#include<typeinfo>
#include<cxxabi.h>
#include<type_traits>
using namespace std;
std::string classname(const std::type_info& info)
{
int status;
char* rslt=abi::__cxa_demangle(info.name(),0,0,&status);
std::string result(rslt);
free(rslt);
return result;
}
int main(int argc, char* argv[])
{
int* ptr = new int(10);
decltype(std::move(ptr)) pval = std::move(ptr);
cout << classname(typeid(pval)) << endl; // as per typeid information the type of pval is int*.
bool isSame = is_same<decltype(pval), int*>::value; // What then is the pval not same as int* as per is_same ?
cout << "isSame status = " << isSame << endl;
cout << is_same<int*, int*>::value << endl;
return(0);
}
The behaviors of
decltype
andtypeid
are different.The exact type of
pval
isint* &&
, i.e. an rvalue-reference toint*
. (That's whystd::is_same
returnsfalse
when comparing it with the type ofint*
.) According to the behavior ofdecltype
,And what
std::move(ptr)
returns is an xvalue.Then given
decltype(std::move(ptr)) pval
, the type ofpval
would beint* &&
.On the other hand, the behavior of
typeid
is different.That means the
std::type_info
object returned bytypeid(pval)
will refer to the referenced type, i.e.int*
, notint* &&
.BTW: What
std::type_info::name
returns is implementation defined.