std::ranges::uninitialized_move, std::ranges::uninitialized_move_result
来自cppreference.com
在标头 <memory> 定义
|
||
调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S1, no-throw-forward-iterator O, no-throw-sentinel-for<O> S2 > |
(1) | (C++20 起) |
template< ranges::input_range IR, no-throw-forward-range OR > requires std::constructible_from<ranges::range_value_t<OR>, |
(2) | (C++20 起) |
辅助类型 |
||
template<class I, class O> using uninitialized_move_result = ranges::in_out_result<I, O>; |
(3) | (C++20 起) |
1) 从输入范围
[ifirst, ilast)
移动 N 元素到输出范围 [ofirst, olast)
(为未初始化内存区域),其中 N 为 min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast)) 。 效果等价于:
for (; ifirst != ilast && ofirst != olast; ++ofirst, ++ifirst) ::new (const_cast<void*>(static_cast<const volatile void*>(std::addressof(*first)))) std::remove_reference_t<std::iter_reference_t<O>>(ranges::iter_move(ifirst));
若在初始化期间抛出异常,则以未指定顺序销毁
[ofirst, olast)
中已构造的元素。而且 [ifirst, ilast)
中已被移动的元素被置于合法但未指定的状态。2) 同 (1) ,但以
in_range
为第一范围并以 out_range
为第二范围,如同以 ranges::begin(in_range) 为 ifirst
,以 ranges::end(in_range) 为 ilast
,以 ranges::begin(out_range) 为 ofirst
,并以 ranges::end(out_range) 为 olast
。此页面上描述的仿函数实体是 niebloid,即:
实际上,它们能以函数对象,或者某些特殊编译器扩展实现。
参阅
ifirst, ilast | - | 要移动的输入元素范围 |
ofirst, olast | - | 要初始化的输出范围 |
in_range | - | 要移动的输入元素范围 |
out_range | - | 要初始化的输出范围 |
返回值
{ifirst + N, ofirst + N} 。
复杂度
与 N 成线性。
异常
构造目标范围中的元素时抛出的异常,若存在。
注解
若输出范围的值类型为平凡类型 (TrivialType) ,则实现可能提升 ranges::uninitialized_move
的效率,例如用 ranges::copy_n 。
可能的实现
struct uninitialized_move_fn { template <std::input_iterator I, std::sentinel_for<I> S1, no-throw-forward-iterator O, no-throw-sentinel-for<O> S2> requires std::constructible_from<std::iter_value_t<O>, std::iter_rvalue_reference_t<I>> ranges::uninitialized_move_result<I, O> operator()( I ifirst, S1 ilast, O ofirst, S2 olast ) const { O current {ofirst}; try { for (; !(ifirst == ilast or current == olast); ++ifirst, ++current) ::new (const_cast<void*>(static_cast<const volatile void*> (std::addressof(*current)))) std::remove_reference_t< std::iter_reference_t<O>>(ranges::iter_move(ifirst)); return {std::move(ifirst), std::move(current)}; } catch (...) { // 回滚:析构已构造的元素 for (; ofirst != current; ++ofirst) ranges::destroy_at(std::addressof(*ofirst)); throw; } } template <ranges::input_range IR, no-throw-forward-range OR> requires std::constructible_from<ranges::range_value_t<OR>, ranges::range_rvalue_reference_t<IR>> ranges::uninitialized_move_result<ranges::borrowed_iterator_t<IR>, ranges::borrowed_iterator_t<OR>> operator()( IR&& in_range, OR&& out_range ) const { return (*this)(ranges::begin(in_range), ranges::end(in_range), ranges::begin(out_range), ranges::end(out_range)); } }; inline constexpr uninitialized_move_fn uninitialized_move{}; |
示例
运行此代码
#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[] { "Home", "World" }; 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(std::begin(in), std::end(in), 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: "Home" "World" after move, in: "" "" after move, out: "Home" "World"
参阅
(C++20) |
移动一定量对象到未初始化的内存区域 (niebloid) |
(C++17) |
移动一个范围的对象到未初始化的内存区域 (函数模板) |