std::uninitialized_move_n
来自 zh.cppreference.net
| 定义于头文件 <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) 从源范围起始于
d_first 的前 count 个元素构造目标范围起始于 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 + [0, count) 中的部分对象将处于有效但未指定的状态,且已构造的对象将以未指定的顺序销毁。2) 同 (1),但根据
policy 执行。 此重载仅当以下表达式的值为
true 时才参与重载决议:
|
|
(至C++20止) |
|
|
(自C++20起) |
|
若 |
(自C++20起) |
参数
| first | - | 要移动元素范围的起始位置 |
| d_first | - | 目标范围的起始位置 |
| count | - | 要移动的元素数量 |
| 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("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++标准。
| DR | 应用于 | 已发布行为 | 正确行为 |
|---|---|---|---|
| LWG 3918 | C++17 | 移动构造始终执行,即使 输入迭代器解引用为纯右值 |
不进行移动构造 在这种情况下 |
参见
|
(C++17)
|
将对象范围移动到未初始化的内存区域
(函数模板) |
|
(C++11)
|
将指定数量的对象复制到未初始化的内存区域
(函数模板) |
|
(C++20)
|
将指定数量的对象移动到未初始化的内存区域
(算法函数对象) |