Search code examples
c++language-lawyerallocator

Allocator named requirements -- exceptions


[allocator.requirements.general]/37

Throws: allocate may throw an appropriate exception.

Any limitations on "appropriate" implied elsewhere?

Can a valid custom allocator just throw a double on any request?


Context: implementation of a noexcept function that uses allocator, but has fallback strategy to do something if all allocations fail.


Solution

  • Any limitations on "appropriate" implied elsewhere?

    No. "Appropriate" qualifier has no objective meaning. It's effectively a suggestion to use common sense. There are no limitations on the type thrown from allocate.

    Can a valid custom allocator just throw a double on any request?

    It would be conforming if the author considers double to be appropriate. I wouldn't consider it "appropriate" myself, but it's up to the author to decide.

    Context: implementation of a noexcept function that uses allocator, but has fallback strategy to do something if all allocations fail.

    You should use a catch-all block:

    try {
        ptr = a.allocate();
    } catch(...) {
        // deal with it
    }