首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::return_temporary_buffer

Defined in header <memory>

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

(deprecated in C++17)

分配以前分配给std::get_temporary_buffer...

参数

p

-

the pointer previously returned by std::get_temporary_buffer and not invalidated by an earlier call to return_temporary_buffer

返回值

%280%29

Exceptions (none).

(since C++17)

二次

代码语言:javascript
复制
#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));
    // 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>();
    });
 
    std::return_temporary_buffer(p.first);
}

二次

产出:

二次

代码语言:javascript
复制
string
1
test
...

二次

另见

get_temporary_buffer (deprecated in C++17)

obtains uninitialized storage (function template)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券