Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why didn't they add an operator version of iota?

Tags:

c++

c++11

iota

The iota template function was added to the standard library to fill an iterator range 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-specified 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);
        }
    }

would be convenient if you don't want to (or can't) overload operator++() for Tp. I would find this version more widely usable than the default operator++() version. <

like image 460
emsr Avatar asked Jul 08 '11 02:07

emsr


2 Answers

I suspect the reason is the usual mix of one or more of the following reasons:

  • No one submitted a proposal
  • It wasn't considered important enough for this version (which was already huge, and very late)
  • it fell through the cracks and was forgotten (like copy_if in C++98)
  • it is easy to replace using std::generate.
like image 85
jalf Avatar answered Oct 25 '22 22:10

jalf


With lambdas, the second version doesn't 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 implementation of std::iota very redundant:

template<typename ForwardIterator, typename Tp>
void iota(ForwardIterator first, ForwardIterator last, Tp value)
{
  std::generate(first, last, [&value](){return value++;});
}
like image 42
Ben Voigt Avatar answered Oct 25 '22 20:10

Ben Voigt