问题描述
令我惊讶的是,RangeV3中的类似概念的断言失败.
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> >>());
}
为什么?
除其他外,这意味着我不能像使用std::copy
那样使用ranges::copy
算法.
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.
这是RangesV3中通向back_insert
的规范方法吗?
Is this the canonical way to back_insert
in RangesV3?
我在RangeV3中找不到WeaklyIncrementable文档,但是在cppreference中 https ://en.cppreference.com/w/cpp/experimental/ranges/iterator/WeaklyIncrementable ,似乎存在一个未为back_inserter_iterator
定义的有符号不同类型".这可能意味着1或3件事,a)RangeV3过度约束了copy
要求b)copy
并非用于向后插入的算法,c)我不知道如何使用RangeV3.
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.
找到了此 https://github.com/ericniebler/range-v3/issues/867 ,可能的解决方法是使用range::back_inserter(v)
而不是std::back_inserter(v)
.似乎某处存在默认的可构造性要求.
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.
推荐答案
ranges::copy
似乎有一些意外的需求(对我而言). 因此RangesV3提供了替代品ranges::back_inserter
.
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.
例如,我必须改编一个新的迭代器以替换std::ostream_iterator
来创建一些人工函数,包括默认构造函数:
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;}
};
使用此ranges::copy(first, last, ranges_ostream_iterator<int>(std::cout))
可以工作,而ranges::copy(first, last, std::ostream_iterator<int>(std::cout))
不能.
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.
这篇关于为什么在RangeV3中std :: back_inserter_iterator无法弱增量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!