-fno-exceptions and operator new

Suppose a class Foo has a user-supplied operator new, that can return null.
The standard permits this behavior if operator new is marked as 'noexcept'
or 'throw()'.

If operator new cannot throw, then when we 'new Foo' and the operator
returns null, Clang generates code to detect that case and skips calling
the object constructor.

Now suppose operator new is NOT marked 'noexcept' but instead we compile
the program with -fno-exceptions; it turns out that Clang treats this
operator new as if it could throw--that is, it will not detect the null
return value and calls the constructor with a null 'this' pointer (with
predictable consequences). Shouldn't this case be equivalent to marking
our operator new with 'noexcept', and check for the null return value?


No. The basic design of -fno-exceptions is “the user promises there arenever any exceptions being thrown”; it does not mean exactly the same as
putting a noexcept specification on every function and call. If null is a valid
result of your operator new, put a noexcept specification on it.