名前空間
変種
操作

std::return_temporary_buffer

From cppreference.com
< cpp‎ | memory
 
 
メモリ管理ライブラリ
(説明用*)
未初期化メモリのアルゴリズム
(C++17)
(C++17)
(C++17)
制約付き未初期化
メモリアルゴリズム
Cライブラリ

アロケータ
メモリリソース
ガベージコレクションのサポート
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
(C++11)(C++23まで)
未初期化ストレージ
(C++20まで*)
(C++20まで*)
return_temporary_buffer
(C++20まで*)
明示的な生存期間管理
 
ヘッダ <memory> で定義
template< class T >
void return_temporary_buffer( T* p );
(C++17で非推奨)
(C++20で削除)

p が参照するストレージを解放します。

p が、以前の std::get_temporary_buffer の呼び出しによって返されたポインタ値ではない場合、またはその間に std::return_temporary_buffer の呼び出しによって無効化されている場合、動作は未定義です。

目次

[編集] パラメータ

p - 解放するストレージを参照するポインタ

[編集] 戻り値

(なし)

[編集] 例外

何もスローしません。

[編集]

#include <algorithm>
#include <iostream>
#include <iterator>
#include <memory>
#include <string>
 
int main()
{
    const std::string s[] = {"string", "1", "test", "..."};
    const auto p = std::get_temporary_buffer<std::string>(4);
    // requires that p.first is passed to return_temporary_buffer
    // (beware of early exit points and exceptions), or better use:
    std::unique_ptr<std::string, void(*)(std::string*)> on_exit(p.first,
    [](std::string* p)
    {
        std::cout << "returning temporary buffer...\n";
        std::return_temporary_buffer(p);
    });
 
    std::copy(s, s + p.second,
              std::raw_storage_iterator<std::string*, std::string>(p.first));
    // has same effect as: std::uninitialized_copy(s, s + p.second, p.first);
    // requires that each string in p is individually destroyed
    // (beware of early exit points and exceptions)
 
    std::copy(p.first, p.first + p.second,
              std::ostream_iterator<std::string>{std::cout, "\n"});
 
    std::for_each(p.first, p.first + p.second, [](std::string& e)
    {
        e.~basic_string<char>();
    }); // same as: std::destroy(p.first, p.first + p.second);
 
    // manually reclaim memory if unique_ptr-like technique is not used:
    // std::return_temporary_buffer(p.first);
}

出力

string
1
test
...
returning temporary buffer...

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2072 C++98 std::get_temporary_buffer によって確保されたストレージ
複数回解放される可能性があった
この場合の動作は
未定義となる

[編集] 関連項目

(C++17で非推奨)(C++20で削除)
未初期化ストレージを取得します
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)