Search code examples
c++visual-studio-2015decltype

Deducing type from pointer using decltype


VS2015 is throwing a lot of errors when trying to execute this code:

int a = 5;
int *p = &a;
std::vector<decltype(*p)> v;

However, when I check the type returned by this decltype I get an int!

typeid(decltype(*p)) == typeid(int) // returns true

Can anyone explain it to me? I did the workaround by simply dereferencing pointer and decltyping the value I got. But why isn't it possible to do it by dereferencing pointer directly?


Solution

  • As an alternative to the solution proposed by @Brian, you can use:

    std::vector<std::remove_reference<decltype(*p)>::type> v;
    

    You can also use:

    std::vector<std::remove_pointer<decltype(p)>::type> v;
    

    or

    std::vector<std::decay<decltype(*p)>::type> v3;