std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result
定义于头文件 <memory> |
||
调用签名 (Call signature) |
||
template< std::input_iterator I, no-throw-forward-iterator O, no-throw-sentinel-for<O> S > |
(1) | (C++20 起) (C++26 起为 constexpr) |
辅助类型 |
||
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。
特性测试宏 | 值 | 标准 | 特性 |
---|---|---|---|
__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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象 | 保持不允许 |
[编辑] 参阅
(C++20) |
将一系列对象移动到未初始化内存区域 (算法函数对象) |
(C++17) |
将多个对象移动到未初始化内存区域 (函数模板) |