std::uninitialized_copy
来自 cppreference.cn
定义于头文件 <memory> |
||
template< class InputIt, class NoThrowForwardIt > NoThrowForwardIt uninitialized_copy( InputIt first, InputIt last, |
(1) | (constexpr 自 C++26 起) |
template< class ExecutionPolicy, class ForwardIt, class NoThrowForwardIt > |
(2) | (自 C++17 起) |
1) 从范围
[
first,
last)
复制元素到始于 d_first 的未初始化内存区域,如同通过
for (; first != last; ++d_first, (void) ++first)
::new (voidify
(*d_first))
typename std::iterator_traits<NoThrowForwardIt>::value_type(*first);
如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
2) 与 (1) 相同,但根据 policy 执行。
仅当满足以下所有条件时,此重载才参与重载决议
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 起) |
内容 |
[edit] 参数
first, last | - | 定义要复制的元素的范围的迭代器对 |
d_first | - | 目标范围的起始 |
policy | - | 要使用的执行策略 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-通过 NoThrowForwardIt 的有效实例进行的递增、赋值、比较或间接引用操作均不得抛出异常。 将 &* 应用于 NoThrowForwardIt 值必须产生指向其值类型的指针。(直到 C++11) |
[edit] 返回值
指向复制的最后一个元素之后元素的迭代器。
[edit] 复杂度
与 first 和 last 之间的距离呈线性关系。
[edit] 异常
带有名为 ExecutionPolicy
的模板形参的重载按如下方式报告错误
- 如果作为算法一部分调用的函数的执行抛出异常,并且
ExecutionPolicy
是标准策略之一,则调用 std::terminate。对于任何其他ExecutionPolicy
,行为是实现定义的。 - 如果算法无法分配内存,则抛出 std::bad_alloc。
[edit] 注解
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 用于 专门的内存算法,(1) |
[edit] 可能的实现
template<class InputIt, class NoThrowForwardIt> constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last, NoThrowForwardIt d_first) { using T = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = d_first; try { for (; first != last; ++first, (void) ++current) ::new (static_cast<void*>(std::addressof(*current))) T(*first); return current; } catch (...) { for (; d_first != current; ++d_first) d_first->~T(); throw; } } |
[edit] 示例
运行此代码
#include <cstdlib> #include <iostream> #include <memory> #include <string> int main() { const char *v[] = {"This", "is", "an", "example"}; auto sz = std::size(v); if (void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first = static_cast<std::string*>(pbuf); auto last = std::uninitialized_copy(std::begin(v), std::end(v), first); for (auto it = first; it != last; ++it) std::cout << *it << '_'; std::cout << '\n'; std::destroy(first, last); } catch (...) {} std::free(pbuf); } }
输出
This_is_an_example_
[edit] 缺陷报告
以下行为更改缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 866 | C++98 | 给定 T 作为 NoThrowForwardIt 的值类型,如果T::operator new 存在,则程序可能是不良形式的 |
使用全局替换式 new 代替 |
LWG 2133 | C++98 | 效果描述使用了 for 循环,迭代为 表达式 ++d_first, ++first,这导致 对 operator, 进行实参依赖查找 |
丢弃一个操作数的值 丢弃一个操作数的值 禁用该 ADL |
LWG 2433 | C++11 | 此算法可能被重载的 operator& 劫持 | 使用 std::addressof |
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
[edit] 参见
(C++11) |
将多个对象复制到未初始化的内存区域 (函数模板) |
(C++20) |
将某个范围的对象复制到未初始化的内存区域 (算法函数对象) |