c++range-v3

How do I create a range from a begin and end iterator?


I have an object with functions for getting the begin and end iterators:

const_iterator err_begin() const
const_iterator err_end() const 

Because they are not named begin and end, I cannot pass my object directly to functions in range-v3.

Is there a simple wrapper I can use to make this object work with the range-v3 library?

For example:

auto hasErrors = !empty(something(x.err_begin(), x.err_end())); 

Solution

  • If you're still using range-v3, you'll want iterator_range:

    auto hasErrors = !empty(ranges::make_iterator_range(x.err_begin(), x.err_end()));
    

    For C++20 onwards, you can use subrange:

    auto hasErrors = !empty(std::ranges::subrange{x.err_begin(), x.err_end()});