名前空間
変種
操作

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

From cppreference.com
< 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以降)

Let N be ranges::min(ranges::distance(ifirst, ilast), ranges::distance(ofirst, olast)).

1) 範囲[ifirstilast) から、未初期化メモリ領域[ofirstolast)N 個の要素を構築します。これは次のように行われます。

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));
と同等です。

このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、

目次

[編集] パラメータ

ifirst, ilast - コピー元の要素の範囲を定義するイテレータ・センチネルペア
in_range - コピー元の要素のrange
ofirst, olast - 宛先の範囲を定義するイテレータ・センチネルペア
out_range - 宛先のrange

[編集] 戻り値

上記の通り。

[編集] 計算量

𝓞(N)

[編集] 例外

変換先範囲の要素の構築時にスローされる例外。

[編集] 注釈

宛先範囲の要素型がTrivialTypeである場合、実装はranges::uninitialized_copyの効率を向上させることができます。

機能テストマクロ 規格 機能
__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 ストレージ上にオブジェクトを作成する可能性があります。 禁止されたままです。

[編集] 関連項目

指定された数のオブジェクトを未初期化メモリ領域にコピーします
(アルゴリズム関数オブジェクト)[編集]
オブジェクトの範囲を未初期化メモリ領域にコピーします
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)