C++ Logo


Advanced search

Subject: Why max_size() is a member rather than a static method?
From: Adam Badura (adam.f.badura_at_[hidden])
Date: 2021-01-14 00:50:47

My assumption was always that max_size() is there to take care of pointer
difference representation. So that the containers can limit their size to a
value which would make the

c.end() - c.begin()

still representable in std::ptrdiff_t. (And also this happens to be the
hint provided by cppreference.com:

However, this could be handled by a constexpr static max_size() method.
While it is a member method instead and becomes constexpr only since C++20.

Using Compiler Explorer (https://godbolt.org/z/E53bYz) I checked how - for
example - GCC behaves here. And there is a strange effect... It seems up to
GCC 8.3 we have:

std::string{}.max_size() = 7fffffffffffffff
std::u32string{}.max_size() = 1fffffffffffffff

while starting with GCC 9.1 we have:

std::string{}.max_size() = 3fffffffffffffff
std::u32string{}.max_size() = fffffffffffffff

So, what are the practical and hypothetical uses of max_size() from the
library point of view? Are there known implementations using it in a way
that couldn't be a constexpr static instead of a method?

Adam Badura

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups