跳到主要内容

std::ranges::uninitialized_move() 算法

// (1)
uninitialized_move_result<I, O>
uninitialized_move( I ifirst, S1 ilast, O ofirst, S2 olast );

// (2)
uninitialized_move_result< ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR> >
uninitialized_move( IR&& in_range, OR&& out_range );

参数类型是泛型的,并具有以下约束:

此外,每个重载都有以下约束

  • (1) std::constructible_from< std::iter_value_t<O>, std::iter_rvalue_reference_t<I> >
  • (1) std::constructible_from< ranges::range_value_t<OR>, ranges::range_rvalue_reference_t<IR> >

辅助类型定义如下:

template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
  • (1)N 个元素从输入范围 [ifirst; ilast) 移动到输出范围 [ofirst; olast),其中 Nmin(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

    此函数的效果等同于

    for (; ifirst != ilast && ofirst != olast; ++ofirst, ++ifirst)
    ::new (static_cast<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) 作为 ifirstranges::end(in_range) 作为 ilastranges::begin(out_range) 作为 ofirst,以及 ranges::end(out_range) 作为 olast

本页描述的函数类实体是niebloids

参数

ifirst
ilast

要从中移动元素的输入范围。

in_range

要从中移动元素的输入范围。

ofirst
olast

要初始化的目标范围。

out_range

要初始化的目标范围。

返回值

{
ifirst + N,
ofirst + N
}

复杂度

时间复杂度为 N 的线性。

异常

构造目标范围中的元素时抛出的异常(如果有)。

可能的实现

uninitialized_move(1) 和 uninitialized_move(2)
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 (...) // rollback: destroy constructed elements
{
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{};

备注

如果输出范围的值类型是TrivialType,则实现可以提高ranges::uninitialized_move的效率。

示例

Main.cpp
#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"
本文源自此 CppReference 页面。它可能已进行修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。

std::ranges::uninitialized_move() 算法

// (1)
uninitialized_move_result<I, O>
uninitialized_move( I ifirst, S1 ilast, O ofirst, S2 olast );

// (2)
uninitialized_move_result< ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR> >
uninitialized_move( IR&& in_range, OR&& out_range );

参数类型是泛型的,并具有以下约束:

此外,每个重载都有以下约束

  • (1) std::constructible_from< std::iter_value_t<O>, std::iter_rvalue_reference_t<I> >
  • (1) std::constructible_from< ranges::range_value_t<OR>, ranges::range_rvalue_reference_t<IR> >

辅助类型定义如下:

template< class I, class O >
using uninitialized_move_result = ranges::in_out_result<I, O>;
  • (1)N 个元素从输入范围 [ifirst; ilast) 移动到输出范围 [ofirst; olast),其中 Nmin(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

    此函数的效果等同于

    for (; ifirst != ilast && ofirst != olast; ++ofirst, ++ifirst)
    ::new (static_cast<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) 作为 ifirstranges::end(in_range) 作为 ilastranges::begin(out_range) 作为 ofirst,以及 ranges::end(out_range) 作为 olast

本页描述的函数类实体是niebloids

参数

ifirst
ilast

要从中移动元素的输入范围。

in_range

要从中移动元素的输入范围。

ofirst
olast

要初始化的目标范围。

out_range

要初始化的目标范围。

返回值

{
ifirst + N,
ofirst + N
}

复杂度

时间复杂度为 N 的线性。

异常

构造目标范围中的元素时抛出的异常(如果有)。

可能的实现

uninitialized_move(1) 和 uninitialized_move(2)
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 (...) // rollback: destroy constructed elements
{
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{};

备注

如果输出范围的值类型是TrivialType,则实现可以提高ranges::uninitialized_move的效率。

示例

Main.cpp
#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"
本文源自此 CppReference 页面。它可能已进行修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档所做的所有更改。
悬停查看原始许可证。