Why can't I compile next code?
#include <iterator>
struct test_iterator {
using value_type = int;
};
int main()
{
std::iterator_traits<test_iterator>::value_type a = 0;
return a;
}
It seems that all three major compilers(msvc/gcc/clang) cannot compile this code.
As far as I know, std::iterator_traits
works like:
template <class T>
struct iterator_traits {
using value_type = typename T::value_type;
... // other memebers
};
What's wrong here?
From iterator_traits, since C++17:
"If Iterator does not have the five member types difference_type, value_type, pointer, reference, and iterator_category, then this template has no members by any of those names (
std::iterator_traits
is SFINAE-friendly)"
Providing the missing types fixes the error: Demo
C++20 adds new ways to fill members if iterator satisfies some concepts.
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