C++ C++;20:为什么ranges::subrange无法执行唯一的\u ptr?

C++ C++;20:为什么ranges::subrange无法执行唯一的\u ptr?,c++,range,c++20,C++,Range,C++20,我制作了一个自定义实用程序函数,用于从给定范围和起始索引生成子范围 它非常方便,但无法在带有std::unique\u ptr的容器上使用 我的代码: #include <algorithm> #include <ranges> #include <iostream> #include <memory> #include <vector> template <std::ranges::random_access_range R&

我制作了一个自定义实用程序函数,用于从给定范围和起始索引生成子范围

它非常方便,但无法在带有
std::unique\u ptr
的容器上使用

我的代码:

#include <algorithm>
#include <ranges>
#include <iostream>
#include <memory>
#include <vector>

template <std::ranges::random_access_range R>
auto range_from(R r, int t) {
    return std::ranges::subrange(std::begin(r) + t, std::end(r));
}

int main() {
    std::vector<int> v {1, 2, 3, 4};
    std::vector<std::unique_ptr<int>> u;
    u.emplace_back(std::make_unique<int>(1));
    u.emplace_back(std::make_unique<int>(2));
    u.emplace_back(std::make_unique<int>(3));
    u.emplace_back(std::make_unique<int>(4));
    
    std::vector<int> v2 (4);
    std::vector<std::unique_ptr<int>> u2 (4);

    std::ranges::copy(v | std::ranges::views::drop(2), v2.begin()); // OK    
    std::ranges::copy(range_from(v, 2), v2.begin()); // OK
    std::ranges::move(u | std::ranges::views::drop(2), u2.begin()); // OK
    // std::ranges::move(range_from(u, 2), u2.begin()); // FAIL

    
}
编译失败?我如何解决它<代码>标准::范围::视图::删除太冗长

编译器输出提供:

In file included from /opt/wandbox/gcc-head/include/c++/11.0.0/vector:66,
                 from /opt/wandbox/gcc-head/include/c++/11.0.0/functional:62,
                 from /opt/wandbox/gcc-head/include/c++/11.0.0/pstl/glue_algorithm_defs.h:13,
                 from /opt/wandbox/gcc-head/include/c++/11.0.0/algorithm:74,
                 from prog.cc:1:
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >; _ForwardIterator = std::unique_ptr<int>*]':
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/stl_uninitialized.h:332:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<int>*, std::vector<std::unique_ptr<int> > >; _ForwardIterator = std::unique_ptr<int>*; _Tp = std::unique_ptr<int>]'
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/stl_vector.h:558:31:   required from 'std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<int>; _Alloc = std::allocator<std::unique_ptr<int> >]'
prog.cc:25:38:   required from here
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/stl_uninitialized.h:137:72: error: static assertion failed: result type must be constructible from value type of input range
  137 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |  
在/opt/wandbox/gcc-head/include/c++/11.0.0/vector:66中包含的文件中,
来自/opt/wandbox/gcc head/include/c++/11.0.0/functional:62,
从/opt/wandbox/gcc-head/include/c++/11.0.0/pstl/glue_-algorithm_-defs.h:13,
从/opt/wandbox/gcc head/include/c++/11.0.0/算法:74,
从程序cc:1:
/opt/wandbox/gcc head/include/c++/11.0.0/bits/stl_uninitialized.h:在“_forwarditeratorstd::uninitialized_copy”(_InputIterator,_InputIterator,_ForwardIterator,_ForwardIterator)[带_InputIterator=u gnu cxx:u normal迭代器;_ForwardIterator=std::unique_ptr*]:
/opt/wandbox/gcc head/include/c++/11.0.0/bits/stl_uninitialized.h:332:37:从“_forwarditeratorstd::_uninitialized_copy_a”(_InputIterator,_InputIterator,_ForwardIterator,std::allocator&)[带_InputIterator=u gnu cxx:u normal_uiterator:u normal\u iterator;_ForwardIterator=std::unique.\u ptr*_=std::unique\ptr]]
/opt/wandbox/gcc-head/include/c++/11.0.0/bits/stl_-vector.h:558:31:从'std::vector::vector(const std::vector&')[with _-Tp=std::unique_-ptr;_-Alloc=std::allocator]中需要
进度cc:25:38:从这里开始需要
/opt/wandbox/gcc head/include/c++/11.0.0/bits/stl_未初始化。h:137:72:错误:静态断言失败:结果类型必须可以从输入范围的值类型构造
137 | static_assert(是可构造的::值,
|  
哦,这个解决方案也失败了:

// ...
template <std::ranges::random_access_range R>
auto range_from2(R r, int t) {
    return r | std::ranges::views::drop(t);
}

int main() {
// ...
    std::ranges::move(range_from2(u, 2), u2.begin()); // FAIL   
}
/。。。
模板
自动范围从2(R、R、t){
返回r | std::ranges::views::drop(t);
}
int main(){
// ...
std::ranges::move(range_from2(u,2),u2.begin());//失败
}

@Someprogrammerdude我认为没有复制(虽然不确定),因为我在
u
上做
std::ranges::move
,而不是
std::ranges::copy
,这对
u | std::ranges::views::drop(2)
也会失败按值获取
r
。这是一个副本。请尝试
r&
@super-Great,problem-solvedWorks。
// ...
template <std::ranges::random_access_range R>
auto range_from2(R r, int t) {
    return r | std::ranges::views::drop(t);
}

int main() {
// ...
    std::ranges::move(range_from2(u, 2), u2.begin()); // FAIL   
}