std::allocator_traits<Alloc>::max_size
来自 cppreference.cn
| 定义于头文件 <memory> |
||
static size_type max_size( const Alloc& a ) noexcept; |
(C++11 起) (C++20 起为 constexpr) |
|
如果可能,通过调用 a.max_size(),从分配器 a 获取理论上最大可能的分配大小。
如果上述不可行(例如,`Alloc` 没有成员函数 `max_size()`),则返回 std::numeric_limits<size_type>::max() / sizeof(value_type)。
目录 |
[编辑] 参数
| a | - | 要检测的分配器 |
[编辑] 返回值
理论上的最大分配大小。
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
|---|---|---|---|
| LWG 2162 | C++11 | 未要求 `max_size` 为 `noexcept` | 需要 |
| LWG 2466 | C++11 | 理论上的最大分配大小(以字节为单位)作为备用返回 | 返回元素大小 |
[编辑] 示例
运行此代码
#include <iostream> #include <memory> #include <locale> int main() { std::allocator<short> b; std::allocator<int> d; const auto p = std::allocator_traits<decltype(b)>::max_size(b); const auto q = std::allocator_traits<decltype(d)>::max_size(d); std::cout.imbue(std::locale("en_US.UTF-8")); std::cout << std::uppercase << "p = " << std::dec << p << " = 0x" << std::hex << p << '\n' << "q = " << std::dec << q << " = 0x" << std::hex << q << '\n'; }
可能的输出
p = 9,223,372,036,854,775,807 = 0x7,FFF,FFF,FFF,FFF,FFF q = 4,611,686,018,427,387,903 = 0x3,FFF,FFF,FFF,FFF,FFF
[编辑] 另请参阅
| (C++20 前) |
返回支持的最大分配大小 ( std::allocator<T> 的公共成员函数) |