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

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

最終更新日時: 2026年07月22日 16時08分23秒 (JST)
rotarymars が更新

履歴 編集

function template
<memory>

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

template <class T>
void return_temporary_buffer(T* p);

この機能は、C++17から非推奨となり、C++20で削除された。短期的な用途のメモリ領域確保には、alloca()のようなスタックからメモリを確保するなど、他の機能を使用すること。

概要

get_temporary_buffer()関数で確保したメモリ領域を解放する。

要件

pが、先行するget_temporary_buffer()の呼び出しで返され、その後return_temporary_buffer(T*)の呼び出しで無効化されていない値であること。

効果

ポインタpが指す領域を解放する。

戻り値

なし

例外

  • 投げない

非推奨・削除の詳細

get_temporary_buffer()関数とstd::return_temporary_buffer()関数は、関数内での一時的なメモリ確保のために、最適化されたメモリ確保の仕組みを提供することを期待して定義されたが、実際にはどの実装も特別なメモリ確保を行わず、そのために使われてこなかった。

将来的にスタックからメモリ確保をする仕組みが検討されているが、これらの関数は設計として例外安全性やRAIIといったものが考慮されていない。スタックからメモリ確保する機能が入ったとしても、これらの関数の内部を改善することはできないと判断され、非推奨となった。

スタックからメモリ確保する機能は、現在の標準ライブラリにはない。そのため、代わりとしては、配置new構文や、標準外のalloca()関数のような機能を使用すること。

例

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

int main()
{
  // int型のオブジェクトを3つ構築する想定の領域確保を依頼
  std::pair<int*, std::ptrdiff_t> result = std::get_temporary_buffer<int>(3);

  int* p = result.first;
  std::size_t size = static_cast<std::size_t>(result.second);

  std::allocator<int> alloc;
  using traits = std::allocator_traits<std::allocator<int>>;

  // オブジェクトを構築
  for (std::size_t i = 0; i < size; ++i) {
    traits::construct(alloc, p + i);
  }

  // オブジェクトを破棄
  for (std::size_t i = 0; i < size; ++i) {
    traits::destroy(alloc, p + i);
  }

  // 確保した領域を解放
  std::return_temporary_buffer(p);
}

出力

参照

  • Why do I need std::get_temporary_buffer? - Stack Overflow
  • LWG2072 Unclear wording about capacity of temporary buffers get_temporary_buffer()の容量についての規定と併せて、二重解放が未定義動作になること、例外を投げないこと(いずれもC++14まで暗黙的に期待されていたこと)が明確化されている。
  • P0174R2 Deprecating Vestigial Library Parts in C++17
  • P0619R4 Reviewing deprecated facilities of C++17 for C++20
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.