GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::ranges::uninitialized_value_construct - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. memory
  3. uninitialized_value_construct

最終更新日時: 2026年08月18日 11時11分59秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<memory>

std::ranges::uninitialized_value_construct(C++20)

namespace std::ranges {
  template <no-throw-forward-iterator I, no-throw-sentinel<I> S>
    requires default_initializable<iter_value_t<I>>
  I
    uninitialized_value_construct(I first, S last);  // (1) C++20
  template <no-throw-forward-iterator I, no-throw-sentinel<I> S>
    requires default_initializable<iter_value_t<I>>
  constexpr I
    uninitialized_value_construct(I first, S last);  // (1) C++26

  template <no-throw-forward-range R>
    requires default_initializable<range_value_t<R>>
  borrowed_iterator_t<R>
    uninitialized_value_construct(R&& r);            // (2) C++20
  template <no-throw-forward-range R>
    requires default_initializable<range_value_t<R>>
  constexpr borrowed_iterator_t<R>
    uninitialized_value_construct(R&& r);            // (2) C++26

  template <execution-policy Ep,
            random_access_iterator I,
            sized_sentinel_for<I> S>
    requires default_initializable<iter_value_t<I>>
  I
    uninitialized_value_construct(Ep&& exec,
                                  I first, S last);  // (3) C++26

  template <execution-policy Ep,
            sized-random-access-range R>
    requires default_initializable<range_value_t<R>>
  borrowed_iterator_t<R>
    uninitialized_value_construct(Ep&& exec, R&& r); // (4) C++26
}

概要

未初期化領域の範囲 (r、[first, last)) の各要素を値構築する。

  • (1): イテレータ範囲を指定する
  • (2): Rangeを直接指定する
  • (3): (1)の並列アルゴリズム版。実行ポリシーを指定する
  • (4): (2)の並列アルゴリズム版。実行ポリシーを指定する

テンプレートパラメータ制約

  • (1):
    • Iがno-throw-forward-iteratorである
    • SがIに対する例外を投げない番兵である
    • Iの要素型が、デフォルト構築可能である
  • (2):
    • Rがno-throw-forward-rangeである
    • Rの要素型が、デフォルト構築可能である

効果

説明用の関数voidifyがあるとして、

template<class T>
constexpr void* voidify(T& obj) noexcept {
  return addressof(obj);
}

以下と等価である:

for (; first != last; ++first)
  ::new (voidify(*first)) remove_reference_t<iter_reference_t<I>>();
return first;

例外

呼び出すコンストラクタなどから例外が送出された場合、その例外がこの関数の外側に伝播される前に、その時点で構築済のオブジェクトは全て未規定の順序で破棄される。すなわち、例外が送出された場合は初期化対象領域は未初期化のままとなる。

例

基本的な使い方

#include <iostream>
#include <memory>
#include <algorithm>

struct Vector {
  int x, y;
};

int main()
{
  std::allocator<Vector> alloc;

  // メモリ確保。
  // この段階では、[p, p + size)の領域は未初期化
  const std::size_t size = 3;
  Vector* p = alloc.allocate(size);

  // 未初期化領域[p, p + size)の各要素を値構築
  std::ranges::uninitialized_value_construct(std::ranges::subrange{p, p + size});

  // pの領域が初期化され、かつ範囲pの全ての要素が2で埋められているか確認
  std::for_each(p, p + size, [](const Vector& v) {
    std::cout << v.x << ',' << v.y << std::endl;
  });

  // 要素を破棄
  std::ranges::destroy(p, p + size);

  // メモリ解放
  alloc.deallocate(p, size);
}

出力

0,0
0,0
0,0

並列アルゴリズムの例 (C++26)

#include <iostream>
#include <memory>
#include <execution>

int main() {
  std::allocator<int> alloc;
  int* p = alloc.allocate(3);

  // 並列に値初期化
  std::ranges::uninitialized_value_construct(
    std::execution::par, p, p + 3);

  for (int i = 0; i < 3; ++i) {
    std::cout << p[i] << ' ';
  }
  std::cout << std::endl;

  alloc.deallocate(p, 3);
}

出力

0 0 0

バージョン

言語

  • C++20

処理系

  • Clang: 16.0 ✅
  • GCC: 10.2.0 ✅
  • Visual C++: 2019 Update 10 ✅

関連項目

  • uninitialized_value_construct

参照

  • P0896R4 The One Ranges Proposal
  • P3508R0 Wording for "constexpr for specialized memory algorithms"
    • C++26からconstexprがついた
  • P3179R9 C++ parallel range algorithms
  • LWG Issue 3870. Remove voidify
    • C++23で、説明専用関数voidifyの本体がconst_cast/static_castを用いる形からaddressofを返す形に変更され、const記憶域へのオブジェクト構築が禁じられた
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.