Namespaces
Variants

std::uninitialized_move

来自 zh.cppreference.net
 
 
算法库
受约束算法及范围上的算法 (C++20)
受约束算法,例如 ranges::copy, ranges::sort, ...
排序及相关操作
划分操作
(C++11)    

排序操作
二分搜索操作
(适用于已划分范围)
集合操作(适用于已排序范围)
合并操作(适用于已排序范围)
堆操作
最小值/最大值操作
(C++11)
(C++17)
字典序比较操作
排列操作


 
 
定义于头文件 <memory>
template< class InputIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_move( InputIt first, InputIt last,
                                     NoThrowForwardIt d_first );
(1) (自C++17起)
(自C++26起constexpr)
template< class ExecutionPolicy,
          class ForwardIt, class NoThrowForwardIt >
NoThrowForwardIt uninitialized_move( ExecutionPolicy&& policy,
                                     ForwardIt first, ForwardIt last,
                                     NoThrowForwardIt d_first );
(2) (自C++17起)
1) 在始于 d_first 的目标范围中,从源范围 [first, last) 的元素构造元素,如同通过

for (; first != last; ++d_first, (void) ++first)
    ::new (voidify(*d_first))
        typename std::iterator_traits<NoThrowForwardIt>::value_type(deref-move(*iter));
return d_first;

如果初始化期间抛出异常,则 [first, last) 中的某些对象会保持在有效但未指定的状态,而已构造的对象会以未指定的顺序销毁。
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 + [0, std::distance(first, last)) 与 [first, last) 重叠,则行为未定义。

(自C++20起)

参数

first, last - 定义待移动元素 范围 的迭代器对
d_first - 目标范围的起始位置
policy - 使用的 执行策略
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。
-
通过 NoThrowForwardIt 有效实例进行的自增、赋值、比较或解引用操作均不得抛出异常。

返回值

如上所述。

异常

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

注解

当输入迭代器解引用为右值时,std::uninitialized_move的行为与std::uninitialized_copy相同。

功能测试宏 值 标准 功能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr用于特化<memory>算法,(1)

可能的实现

template<class InputIt, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_move(InputIt first, InputIt last,
                                              NoThrowForwardIt d_first)
{
    using ValueType = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    auto current = d_first;
    try
    {
        for (; first != last; ++first, (void) ++current)
        {
            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);
        }
        return current;
    }
    catch (...)
    {
        std::destroy(d_first, current);
        throw;
    }
}

示例

#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", "Work!"};
    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::uninitialized_move(std::begin(in), std::end(in), first);
            
            print("after move, in: ", std::begin(in), std::end(in));
            print("after move, out: ", first, last);
            
            std::destroy(first, last);
        }
        catch (...)
        {
            std::cout << "Exception!\n";
        }
        std::free(out);
    }
}

可能的输出:

initially, in: "Home" "Work!"
after move, in: "" ""
after move, out: "Home" "Work!"

缺陷报告

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

DR 应用于 已发布行为 正确行为
LWG 3918 C++17 移动构造总是执行即使
输入迭代器解引用到纯右值
不进行移动构造
在这种情况下

参见

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