Date: Thu, 14 Jan 2021 07:50:47 +0100
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:
https://en.cppreference.com/w/cpp/string/basic_string/max_size.)
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
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:
https://en.cppreference.com/w/cpp/string/basic_string/max_size.)
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
Received on 2021-01-14 00:51:00