6

I am trying to build a nested iterator template and relied on iterators having various traits like value_type. But as it turns out, not all STL types even return iterators with those traits. For instance:

#include <array>
#include <type_traits>

template <typename T>
using iterator_t = decltype(std::declval<T>().begin());

static_assert(std::is_same_v<iterator_t<std::array<int, 3>>, int*>);

This code compiles and shows that the actual type of the array iterator is int*. In that case, how can I still access traits such as value_type etc?

1
  • 4
    You can access traits using std::iterator_traits<iterator_t>. They are specialized for pointer types. Commented May 26, 2020 at 10:33

2 Answers 2

8

The standard doesn't specify how the iterator should be implemented and what the exact type it should be. It just says that std::array is a ContiguousContainer, meaning that its iterator is ContiguousIterator.

In fact, pointers like int* do satisfy the requirements of the iterator of std::array, so it's quite legitimate for implementation.

You can use std::iterator_traits to get the value_type, like:

std::iterator_traits<std::iterator_t<std::array<int, 3>>>::value_type

It works with pointers too.

Sign up to request clarification or add additional context in comments.

Comments

6

int* is an iterator as it satisfies all the requirements necessary of an iterator.

Informally:

  1. You can deference it (unless it's past the final element of the array).

  2. You can increment it (as it's a pointer in an array).

  3. You can copy int* to another int*.

  4. You can call std::swap with int* as the types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.