jeudi 1 novembre 2018

Need clarification - Unexpected behaviour of std::move on T* type in C++

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);
}

Aucun commentaire:

Enregistrer un commentaire