命名空间
变体
操作

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

来自 cppreference.cn
< cpp‎ | 内存
 
 
内存管理库
(仅作说明*)
未初始化内存算法
(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>
调用签名 (Call signature)
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) (C++20 起)
(C++26 起为 constexpr)
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) (C++20 起)
(C++26 起为 constexpr)
辅助类型
template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
(3) (C++20 起)

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

1)N 个元素从 [ifirstilast) 移动(如果支持则使用移动语义)到未初始化的内存区域 [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

特性测试 标准 特性
__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++ 标准。

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

[编辑] 参见

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