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

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

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

履歴 編集

function template
<memory>

std::make_obj_using_allocator(C++20)

template<class T, class Alloc, class... Args>
  constexpr T make_obj_using_allocator(const Alloc& alloc, Args&&... args);

概要

Alloc 型のアロケータオブジェクト alloc、および、コンストラクタ引数 args を用いて、T 型のオブジェクトを uses-allocator 構築する。

構築対象の型 T は関数引数からは推論できないため、明示的に指定する必要がある。

戻り値

以下と同等

make_from_tuple<T>(uses_allocator_construction_args<T>(alloc, std::forward<Args>(args)...))

備考

  • uses_allocator_construction_args を見ればわかる通り、uses-allocator 構築は、その名前に反して必ずしもアロケータオブジェクトを使うとは限らないので注意。
    (uses_allocator_v<T, Alloc> が false の場合、アロケータオブジェクト alloc は無視される)

例

#include <iostream>
#include <vector>
#include <utility>
#include <memory>
#include <new>

// 状態付きアロケータ
template <typename T>
class MyAlloc {
public:
  using value_type = T;
  T* allocate(std::size_t n) { return static_cast<T*>(::operator new(sizeof(T) * n)); }
  void deallocate(T* p, std::size_t n) { ::operator delete(static_cast<void*>(p), sizeof(T) * n); }
  MyAlloc(int state) noexcept : state(state) {}
  template <typename U>
  MyAlloc(const MyAlloc<U>& o) noexcept : state(o.state) {}
  int state;
};

template <typename T>
bool operator==(const MyAlloc<T>& lhs, const MyAlloc<T>& rhs) noexcept
{
  return lhs.state == rhs.state;
}

template <typename T>
bool operator!=(const MyAlloc<T>& lhs, const MyAlloc<T>& rhs) noexcept
{
  return lhs.state != rhs.state;
}

// 型別名
using VEC = std::vector<int, MyAlloc<int>>;

using V = std::pair<std::pair<VEC, int>, int>;

int main()
{
  auto v = std::make_obj_using_allocator<V>(
    MyAlloc<int>{42}, std::make_pair(VEC{MyAlloc<int>(99)}, 1), 2
  );
  std::cout << v.first.first.get_allocator().state << '\n';
}

出力

42

バージョン

言語

  • C++20

処理系

  • Clang: 9.0.0 ❌
  • GCC: 9.1.0 ✅
  • Visual C++: ??

関連項目

  • uses_allocator_construction_args
  • uninitialized_construct_using_allocator
  • uses_allocator
  • scoped_allocator_adaptor
  • polymorphic_allocator
  • pair

参照

  • P0591R4 Utility functions to implement uses-allocator construction
  • LWG Issue 3185. Uses-allocator construction functions missing constexpr and noexcept
    • C++20で、この関数にconstexprが付加された
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.