Why can't I get value_type from iterator_traits?
The issue here is with the line
const auto foo = cbegin(arr);
cbegin(arr)
is going to return a int const *
(pointer to const int) so applying const
to that with const auto foo
means foo
is a int const * const
(const pointer to const int)
std::iterator_traits
is only specialized for a T*
or T const*
so giving it a T* const
fails since there is no valid specialization.
You can fix this by removing the constness in the declaration of bar
with
const typename std::iterator_traits<std::remove_cv_t<decltype(foo)>>::value_type
or you can change foo
to
auto foo = std::cbegin(arr);
if you are okay with it not being const
.
Indeed the const
is problematic, you do basically:
std::iterator_traits<const int* const>::value_type // incorrect due to the last const
You might fix it by changing it to
std::iterator_traits<const int*>::value_type // Correct
You might use std::decay
or std::remove_cv
for that:
const typename std::iterator_traits<std::remove_cv_t<decltype(foo)>>::value_type
(or drop const
from foo
if relevant).