SpECTRE  v2026.04.01
Loading...
Searching...
No Matches
stl_boilerplate::RandomAccessIterator< Iter, ValueType > Class Template Reference

Base class to generate methods for a random-access iterator. This class takes the derived class and the (optionally const) value_type of the iterator. The exposed value_type alias will always be non-const, as required by the standard, but the template parameter's constness will affect the reference and pointer aliases. More...

#include <StlBoilerplate.hpp>

Public Types

using iterator_category = std::random_access_iterator_tag
using value_type = std::remove_const_t<ValueType>
using reference = ValueType&
using pointer = ValueType*
using difference_type = std::ptrdiff_t

Public Member Functions

pointer operator-> () const
Iter & operator++ ()
Iter operator++ (int)
Iter & operator-- ()
Iter operator-- (int)
Iter & operator-= (const difference_type n)
reference operator[] (const difference_type n) const

Protected Member Functions

 RandomAccessIterator (const RandomAccessIterator &)=default
 RandomAccessIterator (RandomAccessIterator &&)=default
RandomAccessIteratoroperator= (const RandomAccessIterator &)=default
RandomAccessIteratoroperator= (RandomAccessIterator &&)=default

Detailed Description

template<typename Iter, typename ValueType>
class stl_boilerplate::RandomAccessIterator< Iter, ValueType >

Base class to generate methods for a random-access iterator. This class takes the derived class and the (optionally const) value_type of the iterator. The exposed value_type alias will always be non-const, as required by the standard, but the template parameter's constness will affect the reference and pointer aliases.

The derived class must implement operator*, operator+=, operator- (of two iterators), and operator==.

template <typename T>
class EmptyIterator
: public stl_boilerplate::RandomAccessIterator<EmptyIterator<T>, T> {
public:
T& operator*() const { ERROR("Not dereferenceable."); }
EmptyIterator& operator+=(const std::ptrdiff_t /*n*/) { return *this; }
};
// Often these will be friend functions.
template <typename T>
bool operator==(const EmptyIterator<T>& /*a*/, const EmptyIterator<T>& /*b*/) {
return true;
}
template <typename T>
std::ptrdiff_t operator-(const EmptyIterator<T>& /*a*/,
const EmptyIterator<T>& /*b*/) {
return 0;
}

This class is inspired by Boost's iterator_facade, except that that has a lot of special cases, some of which work poorly because they predate C++11.


The documentation for this class was generated from the following file:
  • src/Utilities/StlBoilerplate.hpp