std::allocator_traits<Alloc>::max_size
来自 cppreference.cn
< cpp | memory | allocator traits
定义于头文件 <memory> |
||
static size_type max_size( const Alloc& a ) noexcept; |
(自 C++11 起) (constexpr 自 C++20 起) |
|
若可能,从分配器 a 获取理论上最大的可能分配大小,通过调用 a.max_size()。
若以上不可能(例如,Alloc
不具有成员函数 max_size()
),则返回 std::numeric_limits<size_type>::max() / sizeof(value_type)。
内容 |
[编辑] 参数
a | - | 要检测的分配器 |
[编辑] 返回值
理论最大分配大小。
[编辑] 缺陷报告
以下行为变更缺陷报告被追溯应用到先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
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> 的公共成员函数) |