std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result
定义于头文件 <memory> |
||
调用签名 |
||
template< std::input_iterator I, no-throw-forward-iterator O, no-throw-sentinel-for<O> S > |
(1) | (自 C++20 起) (constexpr 自 C++26 起) |
辅助类型 |
||
template< class I, class O > using uninitialized_move_n_result = ranges::in_out_result<I, O>; |
(2) | (自 C++20 起) |
设 N 为 ranges::min(count, ranges::distance(ofirst, olast))。
从以 ifirst 开头的范围复制 N 个元素(如果支持,则使用移动语义)到未初始化的内存区域 [
ofirst,
olast)
,如同通过 auto ret = ranges::uninitialized_move(std::counted_iterator(std::move(ifirst), count),
std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};。
如果在初始化期间抛出异常,则已在 [
ofirst,
olast)
中构造的对象将以未指定的顺序销毁。此外,输入范围中以 ifirst 开头的已移动的对象将处于有效但不指定的状态。
如果 [
ofirst,
olast)
与 ifirst +
[
0,
count)
重叠,则行为未定义。
此页面上描述的类似函数的实体是 算法函数对象(非正式地称为 niebloids),即
目录 |
[编辑] 参数
ifirst | - | 要从中移动元素的输入范围的开始 |
ofirst, olast | - | 定义要初始化的输出范围的迭代器-哨兵对 |
n | - | 要移动的元素数量 |
[编辑] 返回值
如上所述。
[编辑] 复杂度
𝓞(N)。
[编辑] 异常
在目标范围中构造元素时抛出的任何异常。
[编辑] 注解
如果输出范围的值类型是 TrivialType,则实现可以提高 ranges::uninitialized_move_n
的效率,例如通过使用 ranges::copy_n。
特性测试 宏 | 值 | Std | 特性 |
---|---|---|---|
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr 用于 专用内存算法,(1) |
[编辑] 可能的实现
struct uninitialized_move_n_fn { template<std::input_iterator I, no-throw-forward-iterator O, no-throw-sentinel-for<O> S> requires std::constructible_from<std::iter_value_t<O>, std::iter_rvalue_reference_t<I>> constexpr ranges::uninitialized_move_n_result<I, O> operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const { auto iter = std::counted_iterator(std::move(ifirst), count); auto ret = ranges::uninitialized_move(iter, std::default_sentinel, ofirst, olast); return {std::move(ret.in).base(), ret.out}; } }; inline constexpr uninitialized_move_n_fn uninitialized_move_n{}; |
[编辑] 示例
#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[]{ "No", "Diagnostic", "Required", }; 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::ranges::uninitialized_move_n(std::begin(in), sz, first, last); print("after move, in: ", std::begin(in), std::end(in)); print("after move, out: ", first, last); std::ranges::destroy(first, last); } catch (...) { std::cout << "Exception!\n"; } std::free(out); } }
可能的输出
initially, in: "No" "Diagnostic" "Required" after move, in: "" "" "" after move, out: "No" "Diagnostic" "Required"
[编辑] 缺陷报告
以下行为变更缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
[编辑] 参见
(C++20) |
将对象范围移动到未初始化的内存区域 (算法函数对象) |
(C++17) |
将多个对象移动到未初始化的内存区域 (函数模板) |