Namespaces
Variants

std::allocator_traits<Alloc>:: max_size

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
定义于头文件 <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 要求为 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::locales("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> 的公开成员函数)