Namespaces
Variants

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

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
定义于头文件 <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) (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 起)

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

1) N 个元素从 [ ifirst , ilast ) (若支持则使用移动语义)复制到未初始化内存区域 [ ofirst , olast ) ,其行为等价于:

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) 等价于 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

异常

在目标范围元素构造过程中抛出的任何异常。

注释

当输出范围的 值类型 为平凡类型时,实现可以通过使用 ranges::copy_n 等方式来提升 ranges::uninitialized_move 的执行效率。

功能测试 标准 功能特性
__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 (...) // 回滚:销毁已构造的元素
        {
            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 存储上创建对象 保持禁止状态

参见

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