std::return...rary_buffer(3)
π NAME π
okay bestie, this is std::return...rary_buffer(3) from the C++ Standard Libary manual β yes, the header really spells it βLibary,β and we are respecting the canon. The NAME entry is:
std::return_temporary_buffer - std::return_temporary_buffer
which is giving βI contain multitudes (the same name twice)β energy. Slay.
β¨ Synopsis β¨
Defined in header <memory>:
template< class T > (deprecated in C++17)
void return_temporary_buffer( T* p ); (removed in C++20)
Deallocates the storage referenced by p. Thatβs the whole job, and it does it.
β οΈ Ifpis not a pointer value returned by an earlier call tostd::get_temporary_buffer, or has been invalidated by an interveningstd::return_temporary_buffercall, the behavior is undefined. Not me messing around with random pointers π
πΈ Parameters πΈ
pβ the pointer referring to the storage to be deallocated (the page literally saysdeclloated, and honestly same π).
π«Ά Return value π«Ά
(none) β itβs a whole lot of nothing, bestie. No return value, no drama.
π Exceptions π
Throws nothing. Not a single throw. calm.
π Example π
// Run this code
#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);
}
This example is doing the absolute most: it grabs temporary storage, wraps the cleanup in a std::unique_ptr with a custom deleter so the buffer gets returned even if things go sideways, copies strings in with std::raw_storage_iterator, prints them, manually destroys each string with e.~basic_string<char>(); β same as std::destroy(p.first, p.first + p.second); β and comments that youβd manually reclaim memory with std::return_temporary_buffer(p.first); if youβre not using the unique_ptr-like technique. Responsible queen behavior.
π Output π
string
1
test
...
returning temporary buffer...
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards:
- DR
LWG 2072β Applied to:C++98. Behavior as published: the storage allocated bystd::get_temporary_buffercould be deallocated multiple times. Correct behavior: the behavior is undefined in this case.
π See also π
get_temporary_bufferβ obtains uninitialized storage (deprecated in C++17) (function template) (removed in C++20).
thatβs the page, bestie π«Ά source/version: http://cppreference.com; date: 2024.06.10. go forth and return those temporary buffers with main character energy β¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms