[cfe-dev] -fno-exceptions and operator new

John McCall rjmccall at apple.com
Fri Aug 2 13:26:27 PDT 2013


On Aug 1, 2013, at 12:19 PM, Robinson, Paul <Paul_Robinson at playstation.sony.com> wrote:
> 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 are
never 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.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130802/5fa39fc5/attachment.html>


More information about the cfe-dev mailing list