Search code examples
c++stliteratorc++20contiguous

In C++20, how do I write a contiguous iterator?


C++20 has explicit library support for std::contiguous_iterator_tag. Some STL algorithms (e.g. std::copy) can perform much better on contiguous iterators. However, I'm unclear on exactly how the programmer is supposed to get access to this new functionality.

Let's suppose for the sake of argument that we have a completely conforming C++20 library implementation. And I want to write the simplest possible contiguous iterator.

Here's my first attempt.

#include <iterator>

class MyIterator {
    int *p_;
public:
    using value_type = int;
    using reference = int&;
    using pointer = int*;
    using difference_type = int;
    using iterator_category = std::contiguous_iterator_tag;
    int *operator->() const;
    int& operator*() const;
    int& operator[](int) const;
    MyIterator& operator++();
    MyIterator operator++(int);
    MyIterator& operator--();
    MyIterator operator--(int);
    MyIterator& operator+=(int);
    MyIterator& operator-=(int);
    friend auto operator<=>(MyIterator, MyIterator) = default;
    friend int operator-(MyIterator, MyIterator);
    friend MyIterator operator+(MyIterator, int);
    friend MyIterator operator-(MyIterator, int);
    friend MyIterator operator+(int, MyIterator);
};

namespace std {
    int *to_address(MyIterator it) {
        return it.operator->();
    }
}

static_assert(std::contiguous_iterator<MyIterator>);  // FAILS

This fails on both GCC/libstdc++ and MSVC/STL; but is it supposed to?

For my next attempt, I specialized pointer_traits<MyIterator>. MyIterator isn't actually a pointer, so I didn't put anything in pointer_traits except the one function that the library needs. This is my second attempt:

#include <iterator>

class MyIterator {
    ~~~
};

template<>
struct std::pointer_traits<MyIterator> {
    int *to_address(MyIterator it) {
        return it.operator->();
    }
};

static_assert(std::contiguous_iterator<MyIterator>);  // OK!

Is this how I'm supposed to do it? It feels extremely hacky. (To be clear: my first failed attempt also feels extremely hacky.)

Am I missing some simpler way?

In particular, is there any way for MyIterator itself to warrant that it's contiguous, just using members and friends and stuff that can be defined right there in the body of the class? Like, if MyIterator is defined in some deeply nested namespace, and I don't want to break all the way out to the top namespace in order to open namespace std.


EDITED TO ADD: Glen Fernandes informs me that there is a simpler way — I should just add an element_type typedef, like this! (And I can remove 3 of iterator_traits' big 5 typedefs in C++20.) This is looking nicer!

#include <iterator>

class MyIterator {
    int *p_;
public:
    using value_type = int;
    using element_type = int;
    using iterator_category = std::contiguous_iterator_tag;
    int *operator->() const;
    int& operator*() const;
    int& operator[](int) const;
    MyIterator& operator++();
    MyIterator operator++(int);
    MyIterator& operator--();
    MyIterator operator--(int);
    MyIterator& operator+=(int);
    MyIterator& operator-=(int);
    friend auto operator<=>(MyIterator, MyIterator) = default;
    friend int operator-(MyIterator, MyIterator);
    friend MyIterator operator+(MyIterator, int);
    friend MyIterator operator-(MyIterator, int);
    friend MyIterator operator+(int, MyIterator);
};

static_assert(std::contiguous_iterator<MyIterator>);  // OK!

Also, I've seen some stuff about using member typedef iterator_concept instead of iterator_category. Why might I ever want to provide MyIterator::iterator_concept? (I'm not asking for a full historical explanation here; just a simple best-practice guideline "nah forget about iterator_concept" or "yes provide it because it helps with X" would be nice.)


Solution

  • The last version in my question seems to be the most correct. There is just one subtlety to watch out for:

    • MyIterator::value_type should be the cv-unqualified type of the pointee, such that someone could write value_type x; x = *it;
    • MyIterator::element_type should be the cv-qualified type of the pointee, such that someone could write element_type *ptr = std::to_address(it);

    So for a const iterator, element_type isn't just a synonym for value_type — it's a synonym for const value_type! If you don't do this, things will explode inside the standard library.

    Here's a Godbolt proof-of-concept. (It's not a complete ecosystem, in that really you'd want MyIterator to be implicitly convertible to MyConstIterator, and probably use templates to eliminate some of the repetition. I have a rambly blog post on that subject here.)

    #include <iterator>
    
    class MyIterator {
        int *p_;
    public:
        using value_type = int;
        using element_type = int;
        using iterator_category = std::contiguous_iterator_tag;
        int *operator->() const;
        int& operator*() const;
        int& operator[](int) const;
        MyIterator& operator++();
        MyIterator operator++(int);
        MyIterator& operator--();
        MyIterator operator--(int);
        MyIterator& operator+=(int);
        MyIterator& operator-=(int);
        friend auto operator<=>(MyIterator, MyIterator) = default;
        friend int operator-(MyIterator, MyIterator);
        friend MyIterator operator+(MyIterator, int);
        friend MyIterator operator-(MyIterator, int);
        friend MyIterator operator+(int, MyIterator);
    };
    
    static_assert(std::contiguous_iterator<MyIterator>);  // OK!
    
    class MyConstIterator {
        const int *p_;
    public:
        using value_type = int;
        using element_type = const int;
        using iterator_category = std::contiguous_iterator_tag;
        const int *operator->() const;
        const int& operator*() const;
        const int& operator[](int) const;
        MyConstIterator& operator++();
        MyConstIterator operator++(int);
        MyConstIterator& operator--();
        MyConstIterator operator--(int);
        MyConstIterator& operator+=(int);
        MyConstIterator& operator-=(int);
        friend auto operator<=>(MyConstIterator, MyConstIterator) = default;
        friend int operator-(MyConstIterator, MyConstIterator);
        friend MyConstIterator operator+(MyConstIterator, int);
        friend MyConstIterator operator-(MyConstIterator, int);
        friend MyConstIterator operator+(int, MyConstIterator);
    };
    
    static_assert(std::contiguous_iterator<MyConstIterator>);  // OK!