std::get_temporary_buffer
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <memory> で定義
|
||
template< class T > std::pair< T*, std::ptrdiff_t > get_temporary_buffer( std::ptrdiff_t count ); |
(C++17で非推奨) (C++20で削除) |
|
T 型の隣接したオブジェクトを最大 count 個格納するのに十分であるべき未初期化の連続記憶域を確保します。 この要求には拘束力がなく、処理系は count 個の隣接したオブジェクトを格納するのに必要なよりも多く、あるいは少なく、確保することがあります。
引数
| count | - | 所望のオブジェクト数 |
戻り値
確保した記憶域の先頭を指すポインタと、実際に確保された記憶域に収められるオブジェクトの数を保持する、 std::pair。
メモリを確保できなかった場合、または確保された記憶域が T 型のオブジェクトを1個格納するにも十分でない場合、結果の first 要素はヌルポインタに、 second 要素はゼロになります。
例外
| (なし) | (C++11未満) |
| noexcept 指定: noexcept |
(C++11以上) |
例
Run this code
#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);
}
出力:
string
1
test
...
関連項目
(C++17で非推奨)(C++20で削除) |
未初期化記憶域を解放します (関数テンプレート) |