##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::get_temporary_buffer

From cppreference.com
< cpp‎ | memory

Defined in header `<memory>` |  |
template< class T > std::pair< T*, std::ptrdiff_t > get_temporary_buffer( std::ptrdiff_t count ) |  |

Allocates storage sufficient to store up to `count` adjacent objects of type `T`. If there is insufficient memory for all `count` objects, allocates less than `count`, if possible.

### [edit] Parameters

count | - | the number of objects to allocate

### [edit] Return value

An 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 (may be zero).

### [edit] Exceptions

(none) | (until C++11)
`noexcept` specification: noexcept | (since C++11)

### [edit] Example

[edit]
Run this code

    #include <algorithm>
    #include <iostream>
    #include <memory>
    #include <string>

    int main()
    {
        const std::string s[] = {"string", "1", "test", "..."};
        std::string* p = std::get_temporary_buffer<std::string>(4).first;

        std::copy(std::begin(s), std::end(s),
                  std::raw_storage_iterator<std::string*, std::string>(p));

        for (std::string* i = p; i != p+4; ++i) {
            std::cout << *i << '\n';
            i->~basic_string<char>();
        }
        std::return_temporary_buffer(p);
    }

Output:

    string
    1
    test
    ...

### [edit] See also

return_temporary_buffer | frees uninitialized storage (function template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/get_temporary_buffer&oldid=64844"
Category:

- unconditionally noexcept