std::uninitialized_move_n
定义于头文件 <memory> |
||
template< class InputIt, class Size, class NoThrowForwardIt > std::pair<InputIt, NoThrowForwardIt> |
(1) | (C++17 起) (C++26 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt, class Size, class NoThrowForwardIt > |
(2) | (C++17 起) |
+
[
0,
count)
复制元素(如果支持,使用移动语义)到一个从 d_first 开始的未初始化内存区域,如同通过
for (; count > 0; ++d_first, (void) ++first, --count)
::new (voidify
(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(/* value */);
return {first, d_first};
+
[
0,
count)
中的一些对象会处于有效但未指定的状态,并且已构造的对象将以未指定的顺序销毁。
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true。 |
(C++20 前) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 为 true。 |
(C++20 起) |
如果 d_first |
(C++20 起) |
目录 |
[编辑] 参数
first | - | 要移动元素的范围的起始 |
d_first | - | 目标范围的开头 |
count | - | 要移动元素的数量 |
policy | - | 要使用的 执行策略 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-通过 `NoThrowForwardIt` 的有效实例,不允许递增、赋值、比较或间接引用抛出异常。 |
[编辑] 返回值
如上所述。
[编辑] 复杂度
关于 count 的线性复杂度。
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载会按如下方式报告错误:
- 如果作为算法一部分调用的函数执行抛出异常,并且
ExecutionPolicy
是 标准策略 之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 注意
当输入迭代器解引用为右值时,std::uninitialized_move_n
的行为与 std::uninitialized_copy_n 相同。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 对于未初始化内存算法,(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("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_n(std::begin(in), sz, 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: "One" "Definition" "Rule" after move, in: "" "" "" after move, out: "One" "Definition" "Rule"
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
LWG 3918 | C++17 | 需要额外的临时具现化 当输入迭代器解引用为纯右值时 |
在这种情况下复制元素 |
[编辑] 参阅
(C++17) |
将一系列对象移动到未初始化内存区域 (函数模板) |
(C++11) |
将一定数量的对象复制到未初始化内存区域 (函数模板) |
(C++20) |
将多个对象移动到未初始化内存区域 (算法函数对象) |