static pointer
allocate(Alloc& a, size_type n); // (1) C++11
[[nodiscard]]
static constexpr pointer
allocate(Alloc& a, size_type n); // (1) C++20
static constexpr pointer
allocate(Alloc& a, size_type n); // (1) C++26
static pointer
allocate(Alloc& a, size_type n,
const_void_pointer hint); // (2) C++11
[[nodiscard]]
static constexpr pointer
allocate(Alloc& a, size_type n,
const_void_pointer hint); // (2) C++20
static constexpr pointer
allocate(Alloc& a, size_type n,
const_void_pointer hint); // (2) C++26
概要
メモリを確保する。
戻り値
- (1) :
a.allocate(n)
- (2) :
a.allocate(n, hint)
という式が有効であればそれを呼び出し、そうでなければa.allocate(n)
を呼び出す。
例
#include <memory>
int main()
{
std::allocator<int> alloc;
using traits = std::allocator_traits<decltype(alloc)>;
// 10要素のint領域を確保する
std::size_t n = 10;
int* p = traits::allocate(alloc, n);
// 確保したメモリを解放する
traits::deallocate(alloc, p, n);
}
出力
バージョン
言語
- C++11
処理系
- Clang: 3.0 ✅
- GCC: 4.7.3 ✅
- ICC: ??
- Visual C++: 2012 ✅, 2013 ✅
参照
- P0600R1
[[nodiscard]]
in the Library- C++20で
[[nodiscard]]
が付加された
- C++20で
- P0784R7 More constexpr containers
- C++20で
constexpr
が付加された
- C++20で
- P2422R1 Remove
nodiscard
annotations from the standard library specification- C++26で
[[nodiscard]]
指定が削除された
- C++26で