最終更新日時(UTC):
が更新

履歴 編集

function
<scoped_allocator>

std::scoped_allocator_adaptor::allocate(C++11)

pointer allocate(size_type n);                                        // (1) C++11
[[nodiscard]] pointer allocate(size_type n);                          // (1) C++20

pointer allocate(size_type n, const_void_pointer hint);               // (2) C++11
[[nodiscard]] pointer allocate(size_type n, const_void_pointer hint); // (2) C++20

概要

メモリを確保する。

戻り値

#include <iostream>
#include <vector>
#include <string>
#include <scoped_allocator>

template <class T>
using alloc_t = std::allocator<T>;

// コンテナの要素(Inner)
using string = std::basic_string<
  char,
  std::char_traits<char>,
  alloc_t<char>
>;

// コンテナ(Outer)
template <class T>
using vector = std::vector<
  T,
  std::scoped_allocator_adaptor<alloc_t<T>, alloc_t<typename T::value_type>>
>;

int main()
{
  vector<string>::allocator_type alloc {
    alloc_t<string>(), // vector自体のアロケータオブジェクト
    alloc_t<char>()    // vectorの全ての要素に使用するアロケータオブジェクト
  };

  // 外側のアロケータを使用し、stringが3要素入るメモリを確保
  const std::size_t n = 3;
  string* p = alloc.allocate(n);

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

出力

バージョン

言語

  • C++11

処理系

参照