std::ranges::uninitialized_copy, std::ranges::uninitialized_copy_result
来自 zh.cppreference.net
| 定义于头文件 <memory>
|
||
| 调用签名 |
||
template< std::input_iterator I, std::sentinel_for<I> S1,
/*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S2 >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_result<I, O>
uninitialized_copy( I ifirst, S1 ilast, O ofirst, S2 olast );
|
(1) | (自 C++20) (constexpr 自 C++26) |
template< ranges::input_range IR, /*nothrow-forward-range*/ OR >
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR>>
uninitialized_copy( IR&& in_range, OR&& out_range );
|
(2) | (自 C++20) (constexpr 自 C++26) |
template< /*execution-policy*/ Ep,
std::random_access_iterator I, std::sized_sentinel_for<I> S1,
/*nothrow-random-access-iterator*/ O,
/*nothrow-sized-sentinel-for*/<O> S2 >
requires std::constructible_from<std::iter_value_t<O>,
std::iter_reference_t<I>>
uninitialized_copy_result<I, O>
uninitialized_copy( Ep&& policy, I ifirst, S1 ilast, O ofirst, S2 olast );
|
(3) | (自 C++26) |
template< /*execution-policy*/ Ep, /*sized-random-access-range*/ IR,
/*nothrow-sized-random-access-range*/ OR >
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
uninitialized_copy_result<ranges::borrowed_iterator_t<IR>,
ranges::borrowed_iterator_t<OR>>
uninitialized_copy( Ep&& policy, IR&& in_range, OR&& out_range );
|
(4) | (自 C++26) |
| 辅助类型 |
||
template< class I, class O >
using uninitialized_copy_result = ranges::in_out_result<I, O>;
|
(5) | (自 C++20) |
关于 /*execution-policy*/ 的定义,参见 此页面;关于 /*sized-random-access-range*/ 的定义,参见 此页面;关于其他仅用于阐述的概念的定义,参见 此页面。
1) 在目标范围
[ofirst, olast) 中从源范围 [ifirst, ilast) 的元素构造元素,如同通过
for (; ifirst != ilast && ofirst != olast; ++ofirst, (void)++ifirst)
::new (voidify(*ofirst)) std::remove_reference_t<std::iter_reference_t<O>>(*ifirst);
return {std::move(ifirst), ofirst};
如果在初始化期间抛出异常,则已构造的对象以未指定的顺序被销毁。
如果
[ofirst, olast) 与 [ifirst, ilast) 重叠,则行为未定义。2) 与 (1) 相同,但使用
in_range 作为源范围,out_range 作为目标范围。3,4) 与 (1,2) 相同,但按照
policy 执行。此页面上描述的函数式实体是算法函数对象(非正式地称为niebloids),也就是说:
参数
| ifirst, ilast | - | 定义要复制元素的源范围的迭代器-哨兵对 |
| in_range | - | 要复制元素的范围 |
| ofirst, olast | - | 定义目标范围中元素的迭代器-哨兵对 |
| out_range | - | 目标范围 |
| policy | - | 要使用的执行策略 |
返回值
如上所述。
异常
在构造目标范围内的元素时抛出的任何异常。
3,4) 在执行过程中:
- 如果并行化所需的临时内存资源不可用,std::bad_alloc 会被抛出。
- 如果在通过算法参数访问对象时抛出了未捕获的异常,则行为由执行策略决定(对于标准策略,将调用std::terminate)。
注解
实现可能会提高ranges::uninitialized_copy的效率,如果输出范围的值类型是TrivialType。
| 功能测试宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_parallel_algorithm |
202506L |
(C++26) | 并行范围算法 |
__cpp_lib_raw_memory_algorithms |
202411L |
(C++26) | constexpr针对特化<memory>算法,(1,2)
|
可能的实现
struct uninitialized_copy_fn
{
template<std::input_iterator I, std::sentinel_for<I> S1,
/*nothrow-forward-iterator*/ O, /*nothrow-sentinel-for*/<O> S2>
requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
constexpr ranges::uninitialized_copy_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)
ranges::construct_at(std::addressof(*current), *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, /*nothrow-forward-range*/ OR>
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
constexpr ranges::uninitialized_copy_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::next(ranges::begin(out_range), ranges::end(out_range)));
}
template<ranges::forward_range IR, /*nothrow-forward-range*/ OR>
requires std::constructible_from<ranges::range_value_t<OR>,
ranges::range_reference_t<IR>>
constexpr ranges::uninitialized_copy_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::next(ranges::begin(in_range), ranges::end(in_range)),
ranges::begin(out_range),
ranges::next(ranges::begin(out_range), ranges::end(out_range)));
}
};
inline constexpr uninitialized_copy_fn uninitialized_copy{};
|
示例
运行此代码
#include <cstdlib> #include <iomanip> #include <iostream> #include <memory> #include <string> int main() { const char* v[]{"This", "is", "an", "example"}; if (const auto sz{std::size(v)}; void* pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz)) { try { auto first{static_cast<std::string*>(pbuf)}; auto last{first + sz}; std::ranges::uninitialized_copy(std::begin(v), std::end(v), first, last); std::cout << "{"; for (auto it{first}; it != last; ++it) std::cout << (it == first ? "" : ", ") << std::quoted(*it); std::cout << "};\n"; std::ranges::destroy(first, last); } catch (...) { std::cout << "uninitialized_copy exception\n"; } std::free(pbuf); } }
输出:
{"This", "is", "an", "example"};
参阅
(C++20) |
将若干个对象复制到未初始化的内存区域 (算法函数对象) |
| 将一个范围中的对象复制到未初始化的内存区域 (函数模板) |