命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | memory
 
 
内存管理库
(仅为演示目的*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
约束的未初始化
内存算法
ranges::uninitialized_move
(C++20)
C 语言库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
定义于头文件 <memory>
调用签名
template< std::input_iterator I, std::sentinel_for<I> S1,

          no-throw-forward-iterator O, no-throw-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) (since C++20)
(constexpr since C++26)
template< ranges::input_range IR, no-throw-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) (since C++20)
(constexpr since C++26)
辅助类型
template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
(3) (since C++20)

Nranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

1)[ifirstilast) 复制 N 个元素(如果支持,则使用移动语义)到未初始化的内存区域 [ofirstolast),如同通过以下方式:

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};

如果在初始化期间抛出异常,则在 [ofirstolast) 中已构造的对象将以未指定的顺序销毁。 此外,[ifirstilast) 中已被移动的对象将处于有效但未指定的状态。
2) 等价于 return ranges::uninitialized_move(ranges::begin(in_range), ranges::end(in_range),
                                  ranges::begin(out_range), ranges::end(out_range));

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

目录

[编辑] 参数

ifirst, ilast - 定义要从中移动元素的输入范围的迭代器-哨位对
in_range - 要从中移动元素的输入 range
ofirst, olast - 定义要初始化的输出范围的迭代器-哨位对
out_range - 要初始化的输出 range

[编辑] 返回值

如上所述。

[编辑] 复杂度

N 呈线性关系。

[编辑] 异常

目标范围中元素构造时可能抛出的任何异常。

[编辑] 注解

如果输出范围的值类型是TrivialType,则实现可以提高 ranges::uninitialized_move 的效率,例如,通过使用 ranges::copy_n

特性测试 Std 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 专用内存算法(1,2)

[编辑] 可能的实现

struct uninitialized_move_fn
{
    template<std::input_iterator I, std::sentinel_for<I> S1,
             no-throw-forward-iterator O, no-throw-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, no-throw-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::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"

[编辑] 缺陷报告

以下行为变更的缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3870 C++20 此算法可能在 const 存储上创建对象 保持不允许

[编辑] 参见

将多个对象移动到未初始化的内存区域
(算法函数对象)[编辑]
将一个范围的对象移动到未初始化的内存区域
(函数模板) [编辑]