Reputation: 5093
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?
Upvotes: 2
Views: 3644
Reputation: 206607
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;
Upvotes: 11
Reputation: 119174
decltype(*p)
is int&
, not int
, and you can't have a vector of references. The unfortunately named typeid
doesn't expose this difference since it strips references and cv-qualifiers.
This works though:
std::vector<decltype(a)> v;
Upvotes: 6