requirement on user made containers to be compliant with range-v3

252 Views Asked by At

I was wandering, what are the new requirement for a user defined container to be usable with Range-v3(algorithms...)?

For example, what we need to change in our design(member types, member function...) in the standard sense? How to obey Concepts on Range-v3 algorithms? what member functions do we need to provide? what are the changes for iterators?

what is the replacement for iterators/begin/end?

template<typname T>
struct container
{
    //...
    using value_type = T;
    //...
    using iterator = value_type*;
    using const_iterator = const value_type*;
    //...
    iterator begin() { //... }
    //...
};

it will be great if some can give a canonical container implementation.

what are the new ideas/notions brought by Range-v3? what is the new C++ coding way?

1

There are 1 best solutions below

0
On

As far as I can tell a range itself simply needs to have begin and end members that return a valid iterator. The iterator may be a bit harder to get right. As far as I can tell, the weakest iterator (i.e. the one that needs the least members) needs the following members:

  1. default constructor
  2. copy constructor
  3. operator =
  4. operator ++ (in both pre and post forms)
  5. operator *
  6. operator ==
  7. operator !=
  8. Publicly inherit from a suitable instantiation of std::iterator so it has the right type members available -- not though that this is deprecated in C++17 so you may prefer to add the types by hand instead.

You can statically assert for the concepts and that should help you work out what you need:

static_assert(ranges::Iterator<my_iterator>(), "Not Iterator");