std::uninitialized_default_construct
来自 cppreference.cn
定义于头文件 <memory> |
||
template< class NoThrowForwardIt > void uninitialized_default_construct( NoThrowForwardIt first, |
(1) | (C++17 起) (C++26 起为 constexpr) |
template< class ExecutionPolicy, class NoThrowForwardIt > void uninitialized_default_construct( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
1) 在未初始化内存区域
[
first,
last)
中,通过默认初始化构造 typename std::iterator_traits<NoThrowForwardIt>::value_type 类型的对象,如同
for (; first != last; ++first)
::new (voidify
(*first))
typename std::iterator_traits<NoThrowForwardIt>::value_type;
如果在初始化期间抛出异常,则已构造的对象将以未指定顺序销毁。
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 起) |
目录 |
[编辑] 参数
first, last | - | 定义要初始化的元素范围的迭代器对 |
policy | - | 要使用的 执行策略 |
类型要求 | ||
-NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。 | ||
-通过 `NoThrowForwardIt` 的有效实例,不允许递增、赋值、比较或间接引用抛出异常。 |
[编辑] 复杂度
与 first 和 last 之间的距离呈线性关系。
[编辑] 异常
带有名为 ExecutionPolicy
的模板参数的重载会按如下方式报告错误:
- 若作为算法一部分调用的函数执行抛出异常且 `ExecutionPolicy` 是标准策略之一,则调用 std::terminate。对于任何其他 `ExecutionPolicy`,行为是实现定义的。
- 如果算法未能分配内存,则抛出 std::bad_alloc。
[编辑] 注意
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 对于未初始化内存算法,(1) |
[编辑] 可能的实现
template<class NoThrowForwardIt> constexpr void uninitialized_default_construct(NoThrowForwardIt first, NoThrowForwardIt last) { using Value = typename std::iterator_traits<NoThrowForwardIt>::value_type; NoThrowForwardIt current = first; try { for (; current != last; ++current) { ::new (static_cast<void*>(std::addressof(*current))) Value; } } catch (...) { std::destroy(first, current); throw; } } |
[编辑] 示例
运行此代码
#include <cstring> #include <iostream> #include <memory> #include <string> struct S { std::string m{"Default value"}; }; int main() { constexpr int n{3}; alignas(alignof(S)) unsigned char mem[n * sizeof(S)]; try { auto first{reinterpret_cast<S*>(mem)}; auto last{first + n}; std::uninitialized_default_construct(first, last); for (auto it{first}; it != last; ++it) std::cout << it->m << '\n'; std::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } // For scalar types, uninitialized_default_construct // generally does not zero-fill the given uninitialized memory area. int v[]{1, 2, 3, 4}; const int original[]{1, 2, 3, 4}; std::uninitialized_default_construct(std::begin(v), std::end(v)); // Maybe undefined behavior, pending CWG 1997 to be resolved. // for (const int i : v) // std::cout << i << ' '; // The result is unspecified. std::cout << (std::memcmp(v, original, sizeof(v)) == 0 ? "Unmodified\n" : "Modified\n"); }
可能的输出
Default value Default value Default value Unmodified
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
[编辑] 参阅
在由起始和计数定义的未初始化内存区域中,通过默认初始化构造对象 (函数模板) | |
在由范围定义的未初始化内存区域中,通过值初始化构造对象 (函数模板) | |
通过默认初始化在由范围定义的未初始化内存区域中构造对象 (算法函数对象) |