std::allocator_traits<Alloc>::max_size

Header: <memory>

If possible, obtains the maximum theoretically possible allocation size from the allocator a, by calling a.max_size().

# Declarations

static size_type max_size( const Alloc& a ) noexcept;

(since C++11) (constexpr since C++20)

# Parameters

# Return value

Theoretical maximum allocation size.

# Example

#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';
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2162C++11max_size was not required to be noexceptrequired
LWG 2466C++11theoretical maximum allocation size in bytes was returned as fallbacksize in elements is returned

# See also