What am I missing here? Why can't I use decltype to define the value_type of an iterator? The code below gets inscrutable compile-time errors when I use decltype rather than iterator_traits, but only if I also use value_type to declare a vector.
Visual Studio 2017, C++17 rev. 15.6 Preview
#include <vector>
template<class Ptr >
void foo(Ptr beg) {
*beg = 1; // Cool, babies.
// using value_type = decltype(*beg); // COMPILER ERROR when buf declared below
using value_type = typename std::iterator_traits<Ptr>::value_type;
std::vector<value_type> buf(1); // Remove this and decltype compiles.
}
int main() {
std::vector<int> bar(1);
foo(std::begin(bar));
*(std::begin(bar)) = 1;
return 0;
}
By request...
error C2528: 'const_pointer': pointer to reference is illegal
It's for the same reason when you have:
void foo(int *beg)
then
decltype(*beg)
Does not give you an int. You get an int & here. That's what, essentially, your using declaration ends up getting: a reference, an uninvited hitch-hiker.
If you insist on using decltype, you can do:
using value_type = typename std::remove_reference<decltype(*beg)>::type;
In order to ditch the unwelcome hitch-hiker.
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