Why is std::back_inserter_iterator not WeaklyIncrementable in RangeV3?

后端 未结 1 560
情话喂你
情话喂你 2021-01-06 16:39

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

#include
#include
int main(){
         


        
1条回答
  •  情话喂你
    2021-01-06 17:16

    It looks like there are some unexpected (to me) requirements need by ranges::copy. So RangesV3 provides a drop in replacement 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 replace std::ostream_iterator creating some artificial functions, including a default constructor:

    template
    struct ranges_ostream_iterator : std::ostream_iterator{
        using std::ostream_iterator::ostream_iterator;
        ranges_ostream_iterator() : std::ostream_iterator{std::cout}{} // I have to put something here
        ranges_ostream_iterator& operator++(){std::ostream_iterator::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(std::cout)) work, whereas ranges::copy(first, last, std::ostream_iterator(std::cout)) doesn't.

    0 讨论(0)
提交回复
热议问题