std::ranges::uninitialized_default_construct
来自 cppreference.cn
定义于头文件 <memory> |
||
调用签名 |
||
template< no-throw-forward-iterator I, no-throw-sentinel-for<I> S > requires std::default_initializable<std::iter_value_t<I>> |
(1) | (自 C++20 起) (constexpr 自 C++26 起) |
template< no-throw-forward-range R > requires std::default_initializable<ranges::range_value_t<R>> |
(2) | (自 C++20 起) (constexpr 自 C++26 起) |
1) 在未初始化的内存区域
[
first,
last)
中,通过默认初始化构造类型为 std::iter_value_t<I> 的对象,如同通过
for (; first != last; ++first)
::new (voidify(*first))
std::remove_reference_t<std::iter_reference_t<I>>;
return first;
如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
2) 等价于 ranges::uninitialized_default_construct(ranges::begin(r), ranges::end(r))。
此页面上描述的类似函数的实体是算法函数对象(非正式地称为 niebloids),即
内容 |
[编辑] 参数
first, last | - | 定义要初始化的元素范围的迭代器-哨位对 |
r | - | 要初始化的元素的 range |
[编辑] 返回值
如上所述。
[编辑] 复杂度
与 first 和 last 之间的距离呈线性关系。
[编辑] 异常
目标范围中元素构造时可能抛出的任何异常。
[编辑] 注解
如果默认初始化 std::iter_value_t<I> 对象时未调用任何非平凡的默认构造函数,则实现可以跳过对象构造(而不更改可观察的效果),这可以通过 std::is_trivially_default_constructible 检测到。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 用于 专门化的内存算法,(1,2) |
[编辑] 可能的实现
struct uninitialized_default_construct_fn { template<no-throw-forward-iterator I, no-throw-sentinel-for<I> S> requires std::default_initializable<std::iter_value_t<I>> constexpr I operator()(I first, S last) const { using ValueType = std::remove_reference_t<std::iter_reference_t<I>>; if constexpr (std::is_trivially_default_constructible_v<ValueType>) return ranges::next(first, last); // skip initialization I rollback{first}; try { for (; !(first == last); ++first) ::new (static_cast<void*>(std::addressof(*first))) ValueType; return first; } catch (...) // rollback: destroy constructed elements { for (; rollback != first; ++rollback) ranges::destroy_at(std::addressof(*rollback)); throw; } } template<no-throw-forward-range R> requires std::default_initializable<ranges::range_value_t<R>> constexpr ranges::borrowed_iterator_t<R> operator()(R&& r) const { return (*this)(ranges::begin(r), ranges::end(r)); } }; inline constexpr uninitialized_default_construct_fn uninitialized_default_construct{}; |
[编辑] 示例
运行此代码
#include <cstring> #include <iostream> #include <memory> #include <string> int main() { struct S { std::string m{"▄▀▄▀▄▀▄▀"}; }; constexpr int n{4}; alignas(alignof(S)) char out[n * sizeof(S)]; try { auto first{reinterpret_cast<S*>(out)}; auto last{first + n}; std::ranges::uninitialized_default_construct(first, last); auto count{1}; for (auto it{first}; it != last; ++it) std::cout << count++ << ' ' << it->m << '\n'; std::ranges::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } // Notice that for "trivial types" the uninitialized_default_construct // generally does not zero-fill the given uninitialized memory area. constexpr char sample[]{'A', 'B', 'C', 'D', '\n'}; char v[]{'A', 'B', 'C', 'D', '\n'}; std::ranges::uninitialized_default_construct(std::begin(v), std::end(v)); if (std::memcmp(v, sample, sizeof(v)) == 0) { std::cout << " "; // Maybe undefined behavior, pending CWG 1997 to be resolved: // for (const char c : v) { std::cout << c << ' '; } for (const char c : sample) std::cout << c << ' '; } else std::cout << "Unspecified\n"; }
可能输出
1 ▄▀▄▀▄▀▄▀ 2 ▄▀▄▀▄▀▄▀ 3 ▄▀▄▀▄▀▄▀ 4 ▄▀▄▀▄▀▄▀ A B C D
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
[编辑] 参见
在由起始位置和计数定义的未初始化内存区域中,通过默认初始化构造对象 (算法函数对象) | |
在由范围定义的未初始化内存区域中,通过值初始化构造对象 (算法函数对象) | |
在由起始位置和计数定义的未初始化内存区域中,通过值初始化构造对象 (算法函数对象) | |
在由范围定义的未初始化内存区域中,通过默认初始化构造对象 (函数模板) |