Namespaces
Variants

std::ranges:: uninitialized_move_n, std::ranges:: uninitialized_move_n_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,

no-throw-forward-iterator O, no - throw - sentinel - for < O > S >
requires std:: constructible_from < std:: iter_value_t < O > ,
std:: iter_rvalue_reference_t < I >>
uninitialized_move_n_result < I, O >
uninitialized_move_n ( I ifirst, std:: iter_difference_t < I > count,

O ofirst, S olast ) ;
(1) (C++20 起)
(C++26 起为 constexpr)
辅助类型
template < class I, class O >
using uninitialized_move_n_result = ranges:: in_out_result < I, O > ;
(2) (C++20 起)

N ranges:: min ( count, ranges:: distance ( ofirst, olast ) )

将始于 ifirst 的范围中的 N 个元素(若支持则使用移动语义)复制到未初始化的内存区域 [ ofirst , olast ) ,其效果等同于执行: auto ret = ranges:: uninitialized_move ( std:: counted_iterator ( std :: move ( ifirst ) , count ) ,
std:: default_sentinel , ofirst, olast ) ;
return { std :: move ( ret. in ) . base ( ) , ret. out } ;

如果在初始化过程中抛出异常,那么已经在 [ ofirst , olast ) 范围内构造的对象将以未指定的顺序被销毁。同时,从 ifirst 开始的输入范围内已被移动的对象将处于有效但未指定的状态。

如果 [ ofirst , olast ) ifirst + [ 0 , count ) 存在重叠,则行为未定义。

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

目录

参数

ifirst - 待移出元素的输入范围起始位置
ofirst, olast - 定义待初始化元素输出 范围 的迭代器-哨位对
n - 待移动的元素数量

返回值

如上所述。

复杂度

𝓞(N)

异常

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

注释

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

功能测试 标准 功能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 特殊内存算法 ( 1 )

可能的实现

struct uninitialized_move_n_fn
{
    template<std::input_iterator I,
             no-throw-forward-iterator O, no-throw-sentinel-for<O> S>
        requires std::constructible_from<std::iter_value_t<O>,
                                         std::iter_rvalue_reference_t<I>>
    constexpr ranges::uninitialized_move_n_result<I, O>
        operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
    {
        auto iter = std::counted_iterator(std::move(ifirst), count);
        auto ret = ranges::uninitialized_move(iter, std::default_sentinel, ofirst, olast);
        return {std::move(ret.in).base(), ret.out};
    }
};
inline constexpr uninitialized_move_n_fn uninitialized_move_n{};

示例

#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[]{ "No", "Diagnostic", "Required", };
    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_n(std::begin(in), sz, 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: "No" "Diagnostic" "Required"
after move, in: "" "" ""
after move, out: "No" "Diagnostic" "Required"

缺陷报告

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

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

参见

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