名前空間
変種
操作

std::ranges::uninitialized_copy_n、std::ranges::uninitialized_copy_n_result

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
(C++17)
(C++17)
(C++17)
制約付き未初期化
メモリアルゴリズム
ranges::uninitialized_copy_n
(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,

          no-throw-input-iterator O, no-throw-sentinel-for<O> S >
    requires std::constructible_from<std::iter_value_t<O>,
                                     std::iter_reference_t<I>>
uninitialized_copy_n_result<I, O>
    uninitialized_copy_n( I ifirst, std::iter_difference_t<I> count,

                          O ofirst, S olast );
(1) (C++20以降)
(C++26 以降 constexpr)
ヘルパー型
template< class I, class O >
using uninitialized_copy_n_result = ranges::in_out_result<I, O>;
(2) (C++20以降)

Let N be ranges::min(count, ranges::distance(ofirst, olast))

rangeの先頭から N 個の要素を、ifirst から、初期化されていないメモリ領域 [ofirstolast) へコピーします。これは auto ret = ranges::uninitialized_copy(std::counted_iterator(std::move(ifirst), count),
                                      std::default_sentinel, ofirst, olast);
return {std::move(ret.in).base(), ret.out};
のように行われます。

初期化中に例外がスローされた場合、すでに構築されたオブジェクトは未指定の順序で破棄される。

もし [ofirstolast)ifirst + [0count) が重なっている場合、動作は未定義です。

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

目次

[編集] Parameters

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

[編集] Return value

上記の通り。

[編集] Complexity

𝓞(N)

[編集] Exceptions

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

[編集] Notes

出力 range の value type が TrivialType である場合、実装は例えば ranges::copy_n を使用することで、ranges::uninitialized_copy_n の効率を向上させることができます。

機能テストマクロ 規格 機能
__cpp_lib_raw_memory_algorithms 202411L (C++26) 特殊なメモリアルゴリズムの場合、(1)constexpr です。

[編集] Possible implementation

struct uninitialized_copy_n_fn
{
    template<std::input_iterator I, no-throw-input-iterator O, no-throw-sentinel-for<O> S>
        requires std::constructible_from<std::iter_value_t<O>, std::iter_reference_t<I>>
    constexpr ranges::uninitialized_copy_n_result<I, O>
        operator()(I ifirst, std::iter_difference_t<I> count, O ofirst, S olast) const
    {
        auto iter = std::counted_iterator(std::move(ifirst), count);
        auto ret = ranges::uninitialized_copy(iter, std::default_sentinel, ofirst, olast);
        return {std::move(ret.in).base(), ret.out};
    }
};
 
inline constexpr uninitialized_copy_n_fn uninitialized_copy_n{};

[編集] Example

#include <iomanip>
#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    const char* stars[]{"Procyon", "Spica", "Pollux", "Deneb", "Polaris"};
 
    constexpr int n{4};
    alignas(alignof(std::string)) char out[n * sizeof(std::string)];
 
    try
    {
        auto first{reinterpret_cast<std::string*>(out)};
        auto last{first + n};
        auto ret{std::ranges::uninitialized_copy_n(std::begin(stars), n, first, last)};
 
        std::cout << '{';
        for (auto it{first}; it != ret.out; ++it)
            std::cout << (it == first ? "" : ", ") << std::quoted(*it);
        std::cout << "};\n";
 
        std::ranges::destroy(first, last);
    }
    catch (...)
    {
        std::cout << "uninitialized_copy_n exception\n";
    }
}

出力

{"Procyon", "Spica", "Pollux", "Deneb"};

[編集] See also

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