std::get_temporary_buffer

From cppreference.com
< cpp‎ | memory
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
Dynamic memory management
Smart pointers
(C++11)
(C++11)
(C++11)
(until C++17)
(C++11)
(C++23)
Allocators
Memory resources
Uninitialized storage
get_temporary_buffer
(until C++20)
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
Miscellaneous
(C++20)
(C++11)
(C++11)
 
Defined in header <memory>
template< class T >

std::pair<T*, std::ptrdiff_t>

   get_temporary_buffer( std::ptrdiff_t count );
(until C++11)
template< class T >

std::pair<T*, std::ptrdiff_t>

    get_temporary_buffer( std::ptrdiff_t count ) noexcept;
(since C++11)
(deprecated in C++17)
(removed in C++20)

Allocates uninitialized contiguous storage, which should be sufficient to store up to count adjacent objects of type T. The request is non-binding and the implementation may allocate less or more than necessary to store count adjacent objects.

Parameters

count - the desired number of objects

Return value

A std::pair holding a pointer to the beginning of the allocated storage and the number of objects that fit in the storage that was actually allocated.

If no memory could be allocated, or allocated storage is not enough to store a single element of type T, the first element of the result is a null pointer and the second element is zero.

Notes

This API was originally designed with the intent of providing a more efficient implementation than the general-purpose operator new, but no such implementation was created and the API was deprecated and removed.

Example

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <iterator>
 
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)
 
    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);
 
    std::return_temporary_buffer(p.first);
}

Output:

string
1
test
...

See also

(deprecated in C++17)(removed in C++20)
frees uninitialized storage
(function template)