命名空间
变体
操作

std::ranges::uninitialized_copy, std::ranges::uninitialized_copy_result

来自 cppreference.cn
< cpp‎ | memory
 
 
内存管理库
(仅为演示*)
未初始化内存算法
(C++17)
(C++17)
(C++17)
受约束的未初始化
内存算法
ranges::uninitialized_copy
(C++20)
C 库

分配器
内存资源
垃圾回收支持
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化存储
(直到 C++20*)
(直到 C++20*)
显式生命周期管理
 
在头文件 <memory> 中定义
调用签名
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_reference_t<I>>
uninitialized_copy_result<I, O>

    uninitialized_copy( I ifirst, S1 ilast, O ofirst, S2 olast );
(1) (自 C++20 起)
(自 C++26 起 constexpr)
template< ranges::input_range IR, no-throw-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 起)
(自 C++26 起 constexpr)
辅助类型
template< class I, class O >
using uninitialized_copy_result = ranges::in_out_result<I, O>;
(3) (自 C++20 起)

Nranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast))

1) 从范围 [ifirstilast) 构造 N 个元素到未初始化的内存区域 [ofirstolast),如同通过

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};

如果在初始化期间抛出异常,则已构造的对象将以未指定的顺序销毁。
如果 [ofirstolast)[ifirstilast) 重叠,则行为未定义。
2) 等价于 return ranges::uninitialized_copy(ranges::begin(in_range), ranges::end(in_range),
                                  ranges::begin(out_range), ranges::end(out_range));

此页面上描述的类似函数的实体是算法函数对象(非正式地称为 niebloids),即

目录

[编辑] 参数

ifirst, ilast - 定义要复制来源 范围 的元素的迭代器-哨位对
in_range - 要复制来源元素的 range
ofirst, olast - 定义目标 范围 的元素的迭代器-哨位对
out_range - 目标 range

[编辑] 返回值

如上所述。

[编辑] 复杂度

𝓞(N)

[编辑] 异常

在目标范围中构造元素时抛出的任何异常。

[编辑] 注解

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

特性测试 Std 特性
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 专用内存算法(1,2)

[编辑] 可能的实现

struct uninitialized_copy_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_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, no-throw-forward-range OR>
        requires std::constructible_from<ranges::range_value_t<OR>,
    constexpr ranges::range_reference_t<IR>>
        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::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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3870 C++20 此算法可能在 const 存储上创建对象 保持不允许

[编辑] 参见

将多个对象复制到未初始化的内存区域
(算法函数对象)[编辑]
将一个对象范围复制到未初始化的内存区域
(函数模板) [编辑]