std::ranges::uninitialized_move_n, std::ranges::uninitialized_move_n_result
来自cppreference.com
<tbody>
</tbody>
| 在标头 <memory> 定义
|
||
| 调用签名 |
||
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>> uninitialized_move_n_result<I, O> uninitialized_move_n( I ifirst, std::iter_difference_t<I> count, O ofirst, S olast ); |
(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};
如果初始化中抛出了异常,那么以未指定的顺序销毁已构造的对象。另外,始于 ifirst 的范围中的已被移动的对象被置于合法但未指定的状态。
如果 [ofirst, olast) 与 ifirst + [0, count) 有重叠,那么行为未定义。
此页面上描述的函数式实体是算法函数对象(非正式地称为 niebloid),即:
参数
| 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("起初,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("移动后,in:", std::begin(in), std::end(in));
print("移动后,out:", first, last);
std::ranges::destroy(first, last);
}
catch (...)
{
std::cout << "异常!\n";
}
std::free(out);
}
}
可能的输出:
起初,in:"No" "Diagnostic" "Required"
移动后,in:"" "" ""
移动后,out:"No" "Diagnostic" "Required"
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3870 | C++20 | 此算法可能在 const 存储上创建对象
|
保持禁止 |
参阅
(C++20) |
移动范围中对象到未初始化内存 (算法函数对象) |
(C++17) |
移动若干对象到未初始化内存 (函数模板) |