Why does std::allocator<T>::allocate throw std::length_error?

Hi,

I noticed that for some reason in libc++ the function
std::allocator<T>::allocate throws std::length_error when the allocation size
exceeds the maximum size, wheres the C++ standard [allocator.members] requires
to throw std::bad_alloc or std::bad_­array_­new_­length depending on the version
of the standard.

AFAICT this deviation from the standard was introduced intentionally in the
following commit:

commit 4524d6e73953322eafee72aba62ddebef8f7c5cd
Author: Eric Fiselier <eric@efcs.ca>

    Change allocator<T>::allocate to throw length_error, not bad_alloc

    llvm-svn: 268842

What is the rationale behind this change?

Hi Mikhail,

The following LWG issues are related. This seems to be a maze of twisty little DRs and I’m not surprised libc++ isn’t fully up to date.
https://cplusplus.github.io/LWG/issue3038

https://cplusplus.github.io/LWG/issue3190
https://cplusplus.github.io/LWG/issue3237

It also would not surprise me if that commit from 2016 was due to a simple mixup of the common (but wrong in this case) “length_error” with the uncommon (but IIUC correct) “bad_array_new_length”.
However, also notice that that commit comes right after a patch series implementing <experimental/memory_resource>, which was initially specified to throw length_error from polymorphic_allocator. LWG3237 fixed it to throw bad_array_new_length instead.

Anyway, I’m sure a patch would be welcomed… and then held up for a while as Louis ponders the ABI-break implications. :wink:

my $.02,

–Arthur

Hi Mikhail,

The following LWG issues are related. This seems to be a maze of twisty little
DRs and I'm not surprised libc++ isn't fully up to date.
https://cplusplus.github.io/LWG/issue3038
<https://cplusplus.github.io/LWG/issue3038>
https://cplusplus.github.io/LWG/issue3190
<https://cplusplus.github.io/LWG/issue3190>
https://cplusplus.github.io/LWG/issue3237
<https://cplusplus.github.io/LWG/issue3237>
It also would not surprise me if that commit from 2016 was due to a simple mixup
of the common (but wrong in this case) "length_error" with the uncommon (but
IIUC correct) "bad_array_new_length".
However, also notice that that commit comes right after a patch series
implementing <experimental/memory_resource>, which /was/ initially specified to
throw `length_error` from polymorphic_allocator<T>. LWG3237 fixed it to throw
`bad_array_new_length` instead.

Thanks for the pointers.

Anyway, I'm sure a patch would be welcomed... and then held up for a while as
Louis ponders the ABI-break implications. :wink:

I'll have a look into it.

Yes, a patch would be welcome. We can discuss the ABI implications on the review, but I'm seeing this positively.

Louis

I've already submitted the patch: https://reviews.llvm.org/D110846