Generalizing the iterator pair to a range

Even though the previous example works, it is quite bloated as we need to duplicate the start and step length values. A more generalized solution is to let the range mimic a container, where begin() and end() correspond to the start and stop iterators:

template <typename T>class LinearRange {  using iterator = LinearRangeIterator<T>;public:  LinearRange(T start, T stop, size_t num_values)  : start_{start}  , step_size_{get_step_size(start, stop, num_values)}  , num_values_{num_values}  {}  auto begin()const{ return iterator{start_, step_size_, 0}; }  auto end()const{ return iterator{start_, step_size_, num_values_}; }private:  T start_{};  T step_size_{};  size_t num_values_{};};

Get C++ High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.