std::alloca...te_at_least(3)
πΈ NAME πΈ
std::allocator_traits::allocate_at_least β thatβs the page title, bestie, and weβre in the C++ Standard Libary manual. This whole page is about std::allocator_traits::allocate_at_least, and itβs giving βniche allocator lore but make it cute.β π
β¨ Synopsis β¨
Since C++23 (the original synopsis literally has (since C++23), so weβre keeping that energy):
[[nodiscard]] static constexpr std::allocation_result<pointer,
size_type>
allocate_at_least( Alloc& a, size_type n );
allocate_at_least calls a.allocate_at_least(n) and returns its result if the call is well-formed. Otherwise, it is equivalent to return {a.allocate(n), n};.
allocator_at_least tries to allocate a storage for at least n value_type objects, and provides a fallback mechanism that allocates a storage for exact n objects.
π Parameters π
aβ an allocator used for allocating storage.nβ the lower bound of number of objects to allocate storage for.
π«Ά Return value π«Ά
a.allocate_at_least(n)if it is well-formed.- Otherwise,
std::allocation_result<pointer, size_type>{a.allocate(n), n}.
π₯ Exceptions π₯
Throws what and when the selected allocation function throws. No cap.
π§ Notes π§
The allocate_at_least member function of Allocator types are mainly provided for contiguous containers, e.g. std::vector and std::basic_string, in order to reduce reallocation by making their capacity match the actually allocated size when possible. Because allocate_at_least provides a fallback mechanism, it can be directly used where appropriate.
Given an allocator object a of type Alloc, let result denote the value returned from std::allocator_traits<Alloc>::allocate_at_least(a, n). The storage should be deallocated by a.deallocate(result.ptr, m) (typically called via std::allocator_traits<Alloc>::deallocate(a, result.ptr, m)) in order to avoid memory leak.
The argument m used in deallocation must be not less than n and not greater than result.count, otherwise, the behavior is undefined. Note that n is always equal to result.count if the allocator does not provide allocate_at_least, which means that m is required to be equal to n.
β οΈ That undefined behavior bit is not a vibe. Respect the m bounds, bestie.
Feature-test macro, in all its spreadsheet glory:
__cpp_lib_allocate_at_leastβ value202302Lβ Std(C++23)β featureallocate_at_least etc.
π Example π
This section is incomplete.
Reason: no example.
Bestie, thatβs the whole example section. Itβs giving placeholder energy. π
π See also π
allocate_at_leastallocates uninitialized storage at least as large as requested(C++23)size(public member function ofstd::allocator<T>)
π·οΈ Category π·οΈ
Todo no example
Cute sign-off: this page came from http://cppreference.com, dated 2024.06.10. Bye bestie β go allocate at least as much as you need. π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms