Why is std::back_inserter_iterator not WeaklyIncrementable in RangeV3?

荒凉一梦 提交于 2020-06-11 07:34:12

问题


To my surprise this Concept-like assertion fails in RangeV3.

#include<vector>
#include<range/v3/algorithm/copy.hpp>
int main(){
   static_assert(ranges::WeaklyIncrementable<std::back_insert_iterator<std::vector<double> >>());
}

Why is that?

This, among other things means that I cannot use the ranges::copy algorithm as I use to do with std::copy.

    std::vector<double> w(100);
    std::vector<double> v;
    ranges::copy(
        begin(w), end(w),
        std:back_inserter(v)
    );  // compilation error, concept not fulfilled.

Is this the canonical way to back_insert in RangesV3?


I cannot find the WeaklyIncrementable documentation in RangeV3, but in cppreference https://en.cppreference.com/w/cpp/experimental/ranges/iterator/WeaklyIncrementable it seems that there is a "signed different type" that is probably not defined for back_inserter_iterator. This probably means 1 or 3 things, a) RangeV3 is overconstraining the copy requirements b) copy is not the algorithm for back insertion, c) I have no clue how to use RangeV3.


Found this https://github.com/ericniebler/range-v3/issues/867, a possible workaround it to use range::back_inserter(v) instead of std::back_inserter(v). It seems that there is a default constructibility requirement somewhere.


回答1:


It looks like there are some unexpected (to me) requierements need by ranges::copy. So RangesV3 provides a drop in replacemente ranges::back_inserter that works.

However there are many other iterators in the standard that do not work for the same reason but to which there is no drop-in replacements, so it can get ugly.

For example, I had to adapt a new iterator to replaced std::ostream_iterator creating some artificial functions, including a default constructor:

template<class T>
struct ranges_ostream_iterator : std::ostream_iterator<T>{
    using std::ostream_iterator<T>::ostream_iterator;
    ranges_ostream_iterator() : std::ostream_iterator<T>{std::cout}{} // I have to put something here
    ranges_ostream_iterator& operator++(){std::ostream_iterator<T>::operator++(); return *this;}
    ranges_ostream_iterator& operator++(int){return operator++();}      
    using difference_type = int;
    int operator-(ranges_ostream_iterator const&){return 0;}
};

With this ranges::copy(first, last, ranges_ostream_iterator<int>(std::cout)) work, whereas ranges::copy(first, last, std::ostream_iterator<int>(std::cout)) doesn't.



来源:https://stackoverflow.com/questions/56571907/why-is-stdback-inserter-iterator-not-weaklyincrementable-in-rangev3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!