Namespaces
Variants

std::ranges::uninitialized_move, std::ranges::uninitialized_move_result

来自 zh.cppreference.net
 
 
算法库
受约束算法和范围算法 (C++20)
受约束算法,例如 ranges::copy、ranges::sort、...
排序及相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(在已划分范围上)
集合操作(在已排序范围上)
合并操作(在已排序范围上)
堆操作
最小/最大操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
 
定义于头文件 <memory>
调用签名
template< std::input_iterator I, std::sentinel_for<I> S1,
          /*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S2 >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_rvalue_reference_t<I>>
uninitialized_move_result<I, O>
    uninitialized_move( I ifirst, S1 ilast, O ofirst, S2 olast );
(1) (自 C++20 起)
(constexpr 自 C++26 起)
template< ranges::input_range IR, /*nothrow-forward-range*/ OR >
    requires std::constructible_from<ranges::range_value_t<OR>,
                                     ranges::range_rvalue_reference_t<IR>>
uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                          ranges::borrowed_iterator_t<OR>>
    uninitialized_move( IR&& in_range, OR&& out_range );
(2) (自 C++20 起)
(constexpr 自 C++26 起)
template< /*execution-policy*/ Ep,
          std::random_access_iterator I, std::sized_sentinel_for<I> S1,
          /*nothrow-random-access-iterator*/ O,
          /*nothrow-sized-sentinel-for*/<O> S2 >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_rvalue_reference_t<I>>
uninitialized_move_result<I, O>
    uninitialized_move( Ep&& policy, I ifirst, S1 ilast, O ofirst, S2 olast );
(3) (自 C++26 起)
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ IR,
          /*nothrow-sized-random-access-range*/ OR >
    requires std::constructible_from<ranges::range_value_t<OR>,
                                     ranges::range_rvalue_reference_t<IR>>
uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                          ranges::borrowed_iterator_t<OR>>
    uninitialized_move( Ep&& policy, IR&& in_range, OR&& out_range );
(4) (自 C++26 起)
辅助类型
template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
(5) (自 C++20 起)

关于 /*execution-policy*/ 的定义,参见 此页面;关于 /*sized-random-access-range*/ 的定义,参见 此页面;关于其他仅用于说明的概念的定义,参见 此页面。

1) 如同以下方式从源范围 [ofirst, olast) 中的元素在目标范围 [ifirst, ilast) 中构造元素:

for (; ifirst != ilast && ofirst != olast; ++ofirst, (void)++ifirst)
    ::new (voidify(*ofirst))
        std::remove_reference_t<std::iter_reference_t<O>>(ranges::iter_move(ifirst));
return {std::move(ifirst), ofirst};

如果在初始化过程中抛出异常,则 [ofirst, olast) 中已构造的对象将以未指定的顺序销毁。此外,[ifirst, ilast) 中已被移动的对象将处于有效但未指定的状态。
2) 与 (1) 相同,但使用 in_range 作为源范围,使用 out_range 作为目标范围。
3,4) 与 (1,2) 相同,但按照 policy 执行。

此页面上描述的函数式实体是 算法函数对象(非正式地称为 niebloids),即:

参数

ifirst, ilast - 定义要从其中移动元素的输入范围的迭代器-哨兵对
in_range - 要从其中移动元素的输入范围
ofirst, olast - 定义要初始化元素的输出范围的迭代器-哨兵对
out_range - 要初始化的输出范围
policy - 要使用的执行策略

返回值

如上所述。

异常

在构造目标范围内的元素时抛出的任何异常。

3,4) 在执行过程中:
  • 如果所需的并行化临时内存资源不可用,std::bad_alloc 被抛出。
  • 如果在通过算法参数访问对象时抛出未捕获的异常,则行为由执行策略决定 (对于 标准策略, std::terminate 被调用)。

注解

实现可能会提高 ranges::uninitialized_move (例如使用 ranges::copy) 如果输出范围的值类型是 TrivialType。

特性测试宏 值 标准 特性
__cpp_lib_parallel_algorithm 202506L (C++26) 并行范围算法
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 针对特化 <memory>算法, (1,2)

可能的实现

struct uninitialized_move_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S1,
             /*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S2>
        requires std::constructible_from<std::iter_value_t<O>,
                                         std::iter_rvalue_reference_t<I>>
    constexpr ranges::uninitialized_move_result<I, O>
        operator()(I ifirst, S1 ilast, O ofirst, S2 olast) const
    {
        using ValueType = std::remove_reference_t<std::iter_reference_t<O>>;
        O current{ofirst};
        try
        {
            for (; !(ifirst == ilast or current == olast); ++ifirst, ++current)
                ::new (static_cast<void*>(std::addressof(*current))))
                    ValueType(ranges::iter_move(ifirst));
            return {std::move(ifirst), std::move(current)};
        }
        catch (...) // rollback: destroy constructed elements
        {
            for (; ofirst != current; ++ofirst)
                ranges::destroy_at(std::addressof(*ofirst));
            throw;
        }
    }
    
    template<ranges::input_range IR, /*nothrow-forward-range*/ OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
                                         ranges::range_rvalue_reference_t<IR>>
    constexpr ranges::uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                                                ranges::borrowed_iterator_t<OR>>
        operator()(IR&& in_range, OR&& out_range) const
    {
        return (*this)(ranges::begin(in_range), ranges::end(in_range),
                       ranges::begin(out_range),
                       ranges::next(ranges::begin(out_range), ranges::end(out_range)));
    }
    
    template<ranges::forward_range IR, /*nothrow-forward-range*/ OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
                                         ranges::range_rvalue_reference_t<IR>>
    constexpr ranges::uninitialized_move_result<ranges::borrowed_iterator_t<IR>,
                                                ranges::borrowed_iterator_t<OR>>
        operator()(IR&& in_range, OR&& out_range) const
    {
        return (*this)(ranges::begin(in_range),
                       ranges::next(ranges::begin(in_range), ranges::end(in_range))
                       ranges::begin(out_range),
                       ranges::next(ranges::begin(out_range), ranges::end(out_range)));
    }
};

inline constexpr uninitialized_move_fn uninitialized_move{};

示例

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
void print(auto rem, auto first, auto last)
{
    for (std::cout << rem; first != last; ++first)
        std::cout << std::quoted(*first) << ' ';
    std::cout << '\n';
}
int main()
{
    std::string in[]{"Home", "World"};
    print("initially, in: ", std::begin(in), std::end(in));
    if (constexpr auto sz = std::size(in);
        void* out = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first{static_cast<std::string*>(out)};
            auto last{first + sz};
            std::ranges::uninitialized_move(std::begin(in), std::end(in), first, last);
            print("after move, in: ", std::begin(in), std::end(in));
            print("after move, out: ", first, last);
            std::ranges::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "Exception!\n";
        }
        std::free(out);
    }
}

可能的输出:

initially, in: "Home" "World"
after move, in: "" ""
after move, out: "Home" "World"

另见

移动一定数量的对象到未初始化的内存区域
(算法函数对象)
移动一个范围内的对象到未初始化的内存区域
(函数模板)