Should you specialize std::iterator traits for custom iterators?

280 views
Skip to first unread message

Brett Wilson

unread,
Jul 12, 2017, 4:22:04 PM7/12/17
to cxx
This came up because I've been working on a potential new deque implementation in my spare time so I had some context on std::iterator_traits, and just today I'm reviewing some code that makes a custom iterator object for something.

The short question is whether to tell the code review-ee to specialize std::iterator_traits for the new iterator. 

The long question is whether I'm misunderstanding something and we shouldn't need to do this.

--
Details:

In std::deque there's a lot of insert overrides:
http://en.cppreference.com/w/cpp/container/deque/insert

Including these two:

iterator insert( const_iterator pos, size_type count, const T& value );

template<class InputItvoid insert(iterator pos, InputIt first, InputIt last);

which are ambiguous for containers of integer types. So the documentation says the second one applies only if the InputIt qualifies as an InputIterator.

On Linux the STL implements this check (vis SFINAE):
    std::__is_integer<_InputIt>::__type
and MS does:
    enable_if<_Is_iterator<_InputIt>::value>

Brett Wilson

unread,
Jul 12, 2017, 4:27:31 PM7/12/17
to cxx
Sorry, pressed send to soon. Full email:

I chose to implement is_iterator for my deque, which VS implements in terms of std::iterator_traits<_InputIt>::iterator_category.

Does this mean we should specialize iterator traits to get the iterator_category definition correct? Or does this iterator_category magically get set if I write a custom class that acts like an iterator?

Brett

Brett Wilson

unread,
Jul 12, 2017, 4:30:17 PM7/12/17
to cxx
I'm now realizing that std::iterator_traits will pick up the class' iterator_category typedef. So I guess the answer is your iterator should define this and you should be OK.

Sorry for the noise.

Brett
Reply all
Reply to author
Forward
0 new messages