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::allocator::construct - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. memory
  3. allocator
  4. construct

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<memory>

std::allocator::construct(C++17で非推奨)(C++20で削除)

// C++98
void construct(pointer p, const T& val);

// C++11
template <class U, class... Args>
void construct(U* p, Args&&... args);

この関数は、C++17から非推奨となり、C++20で削除された。代わりにstd::allocator_traits::construct()関数を使用すること。

概要

引数を元にインスタンスを構築する。

効果

  • C++98 : new((void *)p) T(val)
  • C++11 : ::new((void *)p) U(std::forward<Args>(args)...)

戻り値

なし

非推奨・削除の詳細

多くのメンバ関数は、アロケータの実装によらず、共通に定義できるものだった。そのため、アロケータの中間インタフェースであるstd::allocator_traitsクラスに、共通のデフォルト実装を定義することとなった。

特殊なアロケータの実装では、この関数を独自に実装する必要があるかもしれない。その場合はstd::allocator_traitsのデフォルト機能を使用せずに、この関数を独自アロケータのインタフェースとして定義すれば、std::allocator_traitsクラスを介してインタフェースにアクセスしたとしても、その独自実装が使用される。

少なくとも、std::allocatorクラスのあらゆる標準ライブラリの実装において、この関数は特殊な動作をする必要がないため、std::allocator_traitsクラスのデフォルト実装に任せることとなった。

例

#include <iostream>
#include <memory>
#include <utility>

int main()
{
  using value_type = std::pair<int, char>;
  std::allocator<value_type> alloc;

  std::size_t n = 1;
  value_type* p = alloc.allocate(n);

  // メモリ領域にpairオブジェクトを構築
  alloc.construct(p, 3, 'a');

  std::cout << p->first << ", " << p->second << std::endl;

  alloc.destroy(p);
  alloc.deallocate(p, n);
}

出力

3, a

C++11版の処理系対応状況

  • Clang: 3.0 ✅
  • GCC: 4.3.6 ✅
  • Visual C++: ??

参照

  • N2345 Placement Insert for Containers (Revision 2)
  • P0174R2 Deprecating Vestigial Library Parts in C++17
  • P0619R4 Reviewing deprecated facilities of C++17 for C++20
You are visitor 01402520 — 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.