Why didn’t they add the operator version of iota?

The iota template function has been added to the standard library to populate a range of iterators with an increasing sequence of values.

template<typename ForwardIterator, typename Tp> void iota(ForwardIterator first, ForwardIterator last, Tp value) { for (; first != last; ++first) { *first = value; ++value; } } 

Most other templates in <numeric> have versions that accept user-defined operators. Having this:

  template<typename ForwardIterator, typename Tp, typename Operator> void iota(ForwardIterator first, ForwardIterator last, Tp value, Operator op) { for (; first != last; ++first) { *first = value; op(value); } } 

it would be convenient if you do not want (or cannot) overload the ++ () operator for Tp. I would find this version more widely used than the default ++ () version. <

+6
source share
2 answers

With lambdas, the second version does not save much, you can just use std::generate .

 template<typename ForwardIterator, typename Tp, typename Operator> void iota(ForwardIterator first, ForwardIterator last, Tp value, Operator op) { std::generate(first, last, [&value,&op](){auto v = value; op(value); return v;}); } 

In fact, this makes the existing std::iota very redundant:

 template<typename ForwardIterator, typename Tp> void iota(ForwardIterator first, ForwardIterator last, Tp value) { std::generate(first, last, [&value](){return value++;}); } 
+4
source

I suspect the cause is the usual combination of one or more of the following reasons:

  • No one sent an offer
  • It was not considered important enough for this version (which was already huge and very late)
  • it fell through cracks and was forgotten (e.g. copy_if in C ++ 98)
  • it is easy to replace using std::generate .
+5
source

Source: https://habr.com/ru/post/892288/


All Articles