Namespaces
Variants

std:: uninitialized_copy

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
定义于头文件 <memory>
template < class InputIt, class NoThrowForwardIt >

NoThrowForwardIt uninitialized_copy ( InputIt first, InputIt last,

NoThrowForwardIt d_first ) ;
(1) (自 C++26 起为 constexpr)
template < class ExecutionPolicy, class ForwardIt,

class NoThrowForwardIt >
NoThrowForwardIt uninitialized_copy ( ExecutionPolicy && policy,
ForwardIt first, ForwardIt last,

NoThrowForwardIt d_first ) ;
(2) (自 C++17 起)
1) 将范围 [ first , last ) 中的元素复制到始于 d_first 的未初始化内存区域,其行为类似于:

for ( ; first ! = last ; ++ d_first, ( void ) ++ first )
:: new ( voidify ( * d_first ) )
typename std:: iterator_traits < NoThrowForwardIt > :: value_type ( * first ) ;

如果在初始化过程中抛出异常,已构造的对象将以未指定的顺序被销毁。
2) (1) 相同,但根据 policy 执行。
此重载仅在满足以下所有条件时参与重载决议:

std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> true

(C++20 前)

std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> true

(C++20 起)


如果 d_first + [ 0 , std:: distance ( first, last ) ) [ first , last ) 存在重叠,则行为未定义。

(since C++20)

目录

参数

first, last - 定义待复制元素 范围 的迭代器对
d_first - 目标范围的起始位置
policy - 使用的 执行策略
类型要求
-
InputIt 必须满足 LegacyInputIterator 的要求。
-
ForwardIt 必须满足 LegacyForwardIterator 的要求。
-
NoThrowForwardIt 必须满足 LegacyForwardIterator 的要求。
-
NoThrowForwardIt 有效实例进行递增、赋值、比较或解引用操作均不得抛出异常。 NoThrowForwardIt 值应用 & * 必须得到指向其值类型的指针。 (C++11 前)

返回值

指向最后一个被复制元素之后位置的迭代器。

复杂度

first last 之间的距离呈线性关系。

异常

带有名为 ExecutionPolicy 模板参数的重载按以下方式报告错误:

  • 如果作为算法一部分调用的函数执行抛出异常,且 ExecutionPolicy 标准策略 之一,则调用 std::terminate 。对于其他任何 ExecutionPolicy ,其行为由实现定义。
  • 如果算法无法分配内存,则抛出 std::bad_alloc

注释

功能测试 标准 功能
__cpp_lib_raw_memory_algorithms 202411L (C++26) constexpr 用于 专用内存算法 ( 1 )

可能的实现

template<class InputIt, class NoThrowForwardIt>
constexpr NoThrowForwardIt uninitialized_copy(InputIt first, InputIt last,
                                              NoThrowForwardIt d_first)
{
    using T = typename std::iterator_traits<NoThrowForwardIt>::value_type;
    NoThrowForwardIt current = d_first;
    try
    {
        for (; first != last; ++first, (void) ++current)
            ::new (static_cast<void*>(std::addressof(*current))) T(*first);
        return current;
    }
    catch (...)
    {
        for (; d_first != current; ++d_first)
            d_first->~T();
        throw;
    }
}

示例

#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
int main()
{
    const char *v[] = {"This", "is", "an", "example"};
    auto sz = std::size(v);
    if (void *pbuf = std::aligned_alloc(alignof(std::string), sizeof(std::string) * sz))
    {
        try
        {
            auto first = static_cast<std::string*>(pbuf);
            auto last = std::uninitialized_copy(std::begin(v), std::end(v), first);
            for (auto it = first; it != last; ++it)
                std::cout << *it << '_';
            std::cout << '\n';
            std::destroy(first, last);
        }
        catch (...) {}
        std::free(pbuf);
    }
}

输出:

This_is_an_example_

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 866 C++98 给定 T 作为 NoThrowForwardIt 的值类型,若
T :: operator new 存在,则程序可能非良构
改用全局替换
new 操作符
LWG 2133 C++98 效果描述使用了 for 循环,其迭代表达式为
++ d_first, ++ first ,这会导致
参数依赖查找 operator,
弃用其中一个操作数
的值以禁用该 ADL
LWG 2433 C++11 此算法可能被重载的 operator & 劫持 改用 std::addressof
LWG 3870 C++20 此算法可能在 const 存储上创建对象 保持禁止状态

参见

将指定数量的对象复制到未初始化的内存区域
(函数模板)
将对象范围复制到未初始化的内存区域
(算法函数对象)