std:: uninitialized_move
|
定义于头文件
<memory>
|
||
|
template
<
class
InputIt,
class
NoThrowForwardIt
>
NoThrowForwardIt uninitialized_move
(
InputIt first, InputIt last,
|
(1) |
(C++17 起)
(C++26 起为 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
NoThrowForwardIt
>
|
(2) | (C++17 起) |
[
first
,
last
)
复制(若支持则使用移动语义)到以
d_first
起始的未初始化内存区域,其行为等价于:
for
(
;
first
!
=
last
;
++
d_first,
(
void
)
++
first
)
::
new
(
voidify
(
*
d_first
)
)
typename
std::
iterator_traits
<
NoThrowForwardIt
>
::
value_type
(
/* value */
)
;
return
d_first
;
[
first
,
last
)
范围内的部分对象将处于有效但未指定的状态,且已构造的对象将以未指定的顺序被销毁。
|
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
|
(since C++20) |
目录 |
参数
| first, last | - | 定义待移动元素 范围 的迭代器对 |
| d_first | - | 目标范围的起始位置 |
| policy | - | 使用的 执行策略 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
ForwardIt
必须满足
LegacyForwardIterator
的要求。
|
||
-
NoThrowForwardIt
必须满足
LegacyForwardIterator
的要求。
|
||
-
NoThrowForwardIt
有效实例进行的自增、赋值、比较或解引用操作均不得抛出异常。
|
||
返回值
如上所述。
复杂度
与 first 和 last 之间的距离呈线性关系。
异常
带有名为
ExecutionPolicy
模板参数的重载按以下方式报告错误:
-
如果作为算法一部分调用的函数执行抛出异常,且
ExecutionPolicy是某个 标准策略 ,则会调用 std::terminate 。对于其他任何ExecutionPolicy,其行为由实现定义。 - 如果算法无法分配内存,将抛出 std::bad_alloc 异常。
注释
当输入迭代器解引用为右值时,
std::uninitialized_move
的行为与
std::uninitialized_copy
相同。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_raw_memory_algorithms
|
202411L
|
(C++26) | constexpr 用于 专用内存算法 , ( 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++ 标准。
| 缺陷报告 | 适用标准 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 3870 | C++20 | 该算法可能在 const 存储上创建对象 | 保持禁止状态 |
| LWG 3918 | C++17 |
当输入迭代器解引用为纯右值时
需要额外的临时物化 |
在此情况下复制元素 |
参见
|
将对象范围复制到未初始化的内存区域
(函数模板) |
|
|
(C++17)
|
将指定数量的对象移动到未初始化的内存区域
(函数模板) |
|
(C++20)
|
将对象范围移动到未初始化的内存区域
(算法函数对象) |