Namespaces
Variants

std:: get_temporary_buffer

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
get_temporary_buffer
( until C++20* )
( until C++20* )

Garbage collector support (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)
(C++11) (until C++23)
헤더에 정의됨 <memory>
template < class T >

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

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

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

get_temporary_buffer ( std:: ptrdiff_t count ) noexcept ;
(C++11부터)
(C++17에서 사용 중단됨)
(C++20에서 제거됨)

만약 count 가 음수이거나 0이면, 아무 작업도 수행하지 않습니다.

그렇지 않으면, count 개의 인접한 T 타입 객체에 대해 초기화되지 않은 연속 저장 공간을 할당하도록 요청합니다. 이 요청은 비구속적(non-binding)이며, 구현체는 대신 임의의 개수(0을 포함)의 인접한 T 타입 객체에 대한 저장 공간을 할당할 수 있습니다.

오버 얼라인드(over-aligned) 타입 지원 여부는 구현에 따라 정의됩니다.

(C++11부터)

목차

매개변수

count - 원하는 객체의 개수

반환값

A std::pair , 멤버 first 는 할당된 저장 공간의 시작을 가리키는 포인터이고, 멤버 second 는 실제로 할당된 저장 공간에 들어갈 수 있는 객체의 수입니다.

만약 count <= 0 이거나 할당된 저장 공간이 T 타입의 단일 요소를 저장하기에 충분하지 않다면, 결과의 first 멤버는 널 포인터이고 second 멤버는 0입니다.

참고 사항

이 API는 원래 범용 operator new 보다 더 효율적인 구현을 제공하려는 의도로 설계되었으나, 그러한 구현이 만들어지지 않았고 API는 deprecated 상태가 되어 제거되었습니다.

예제

#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);
    // p.first를 return_temporary_buffer에 전달해야 함
    // (조기 종료 지점과 예외에 주의), 또는 더 나은 방법:
    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));
    // std::uninitialized_copy(s, s + p.second, p.first);와 동일한 효과
    // p의 각 string을 개별적으로 파괴해야 함
    // (조기 종료 지점과 예외에 주의)
    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>();
    }); // std::destroy(p.first, p.first + p.second);와 동일
    // unique_ptr과 같은 기법을 사용하지 않으면 수동으로 메모리 회수:
    // std::return_temporary_buffer(p.first);
}

출력:

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

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 수정된 동작
LWG 425 C++98 count <= 0 일 때의 동작이 불명확했음 명확하게 규정됨
LWG 2072 C++98 불충분한 메모리 할당이 허용되지 않았음 허용됨

참고 항목

(C++17에서 사용 중단됨) (C++20에서 제거됨)
초기화되지 않은 저장 공간을 해제함
(함수 템플릿)
[static] (C++23)
할당자를 통해 요청된 크기 이상의 저장 공간을 할당함
( std::allocator_traits<Alloc> 의 public static 멤버 함수)