std::uninitialized_move_n

来自cppreference.com
 
 
算法库
受约束算法及范围上的算法 (C++20)
包含算法例如 ranges::copyranges::sort、...
排序和相关操作
划分操作
(C++11)    

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




 
 
在标头 <memory> 定义
template< class InputIt, class Size, class NoThrowForwardIt >
std::pair<InputIt, NoThrowForwardIt>
    uninitialized_move_n( InputIt first, Size count,
                          NoThrowForwardIt d_first );
(1) (C++17 起)
(C++26 起为 constexpr)
template< class ExecutionPolicy,
          class ForwardIt, class Size, class NoThrowForwardIt >
std::pair<ForwardIt, NoThrowForwardIt>
    uninitialized_move_n( ExecutionPolicy&& policy, ForwardIt first,
                          Size count, NoThrowForwardIt d_first );
(2) (C++17 起)
1) 如同用以下方式以从 first 开始的源范围中的首 count 个元素构造从 d_first 开始的目标范围中的元素:

for (; count > 0; ++d_first, (void) ++first, --count)
    ::new (voidify(*d_first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(deref-move(*iter));
return {first, d_first};

如果初始化中抛出了异常,那么 first + [0count) 中的某些对象会留在合法但未指定的状态,并且以未指定的顺序销毁已构造的对象。
2)(1),但按照 policy 执行。
此重载只有在以下表达式的值是 true 时才会参与重载决议:

std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>

(C++20 前)

std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>

(C++20 起)


如果 d_first + [0count)first + [0count) 有重叠,那么行为未定义。

(C++20 起)

参数

first - 要移动的元素范围起始
count - 要移动的元素个数
d_first - 目标范围的起始
policy - 所用的执行策略
类型要求
-
InputIt 必须满足老式输入迭代器 (LegacyInputIterator)
-
ForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator)
-
NoThrowForwardIt 必须满足老式向前迭代器 (LegacyForwardIterator)
-
通过 NoThrowForwardIt 合法实例的自增、赋值、比较或间接不可抛出异常。

返回值

如上所述。

异常

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

注解

当输入迭代器解引用到右值时, std::uninitialized_move_n 的行为同 std::uninitialized_copy_n

功能特性测试 标准 功能特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr<memory> 专门算法, (1)

可能的实现

template<class InputIt, class Size, class NoThrowForwardIt>
constexpr std::pair<InputIt, NoThrowForwardIt>
    uninitialized_move_n(InputIt first, Size count, NoThrowForwardIt d_first)
{
    using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; count > 0; ++first, (void) ++current, --count) {
            auto addr = static_cast<void*>(std::addressof(*current));
            if constexpr (std::is_lvalue_reference_v<decltype(*first)>)
                ::new (addr) ValueType(std::move(*first));
            else
                ::new (addr) ValueType(*first);
        }
    }
    catch (...)
    {
        std::destroy(d_first, current);
        throw;
    }
    return {first, current};
}

示例

#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[]{"One", "Definition", "Rule"};
    print("起初,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::uninitialized_move_n(std::begin(in), sz, first);
            
            print("移动后,in:", std::begin(in), std::end(in));
            print("移动后,out:", first, last);
            
            std::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "异常!\n";
        }
        std::free(out);
    }
}

可能的输出:

起初,in:"One" "Definition" "Rule" 
移动后,in:"" "" "" 
移动后,out:"One" "Definition" "Rule"

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 3918 C++17 即使在输入迭代器解引用为纯右值时也会进行移动构造 此时不会进行移动构造

参阅

移动范围中对象到未初始化内存
(函数模板 & 算法函数对象)[编辑]
复制若干对象到未初始化内存
(函数模板 & 算法函数对象)[编辑]
移动若干对象到未初始化内存
(算法函数对象) [编辑]