Why isn't std::string::max_size a compile-time constant?

后端 未结 4 1311
情书的邮戳
情书的邮戳 2021-01-03 18:22

std::string provides a max_size() method to determine the maximum number of elements it can contain.

However, to work out the maximum lengt

4条回答
  •  渐次进展
    2021-01-03 18:48

    std::string::max_size() calls std::allocator::max_size() under the hood.

    According to the standard, 20.9.6.1.10:

    size_type max_size() const noexcept;

    Returns: The largest value N for which the call allocate(N,0) might succeed.

    (See also: allocator::max_size)

    Theoretically, an allocator implementation could be able to work out the maximum size of a chunk of memory it could allocate via a syscall. This would help determine the largest possible size for a string, inside a specific process.

提交回复
热议问题