Recently I've noticed that the following statement is not true given std::string s
.
s.max_size() == s.get_allocator().max_size();
I find this interesting, by default std::string
will use std::allocator<char>
which has a theoretical limit of size_type(-1)
(yes i know I'm assuming 2's complement, but that's unrelated to the actual question). I know that the practical limitations will be significantly less than this. On a typical 32-bit, x86 system, the kernel will occupy 2GB (perhaps 1GB) of the address space leaving a much smaller practical upper limit.
Anyway, GNU libstdc++'s std::basic_string<>::max_size()
appears to return the same value regardless of what the allocator it is using says (something like 1073741820
).
So the question remains, why doesn't std::basic_string<>::max_size()
just return get_allocator().max_size()
? It seems to me that this is the hypothetical upper limit. And if the allocation comes up short, it'll just throw a std::bad_alloc
, so why not try?
This is more of a curiosity than anything else, I was just wondering why the two are defined separately in at least this one implementation.