Skip to content

Enable /Zc:throwingNew when building with MSVC#381

Closed
jjuhl wants to merge 1 commit into
texus:1.xfrom
jjuhl:better-msvc-performance
Closed

Enable /Zc:throwingNew when building with MSVC#381
jjuhl wants to merge 1 commit into
texus:1.xfrom
jjuhl:better-msvc-performance

Conversation

@jjuhl

@jjuhl jjuhl commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

From Microsoft's documentation
(https://learn.microsoft.com/en-us/cpp/build/reference/zc-throwingnew-assume-operator-new-throws?view=msvc-170) :

"When the /Zc:throwingNew option is specified, the compiler optimizes calls to operator new to skip checks for a null pointer return. This option tells the compiler to assume that all linked implementations of operator new and custom allocators conform to the C++ standard and throw on allocation failure. By default in Visual Studio, the compiler pessimistically generates null checks (/Zc:throwingNew-) for these calls, because users can link with a non-throwing implementation of operator new or write custom allocator routines that return null pointers."

Nothing in TGUI assumes that new returns nullptr on failure and if any users write an allocator that does so or explicitly link to a non-conforming, non-throwing allocator, then they are advanced enough to know to locally disable this option. We should not penalize the common user to cater to an obscure corner case (and this also better matches behaviour with other compilers). Let's default to the standard compliant, and better performing, behaviour.

From Microsoft's documentation
(https://learn.microsoft.com/en-us/cpp/build/reference/zc-throwingnew-assume-operator-new-throws?view=msvc-170) :

"When the /Zc:throwingNew option is specified, the compiler optimizes
calls to operator new to skip checks for a null pointer return. This
option tells the compiler to assume that all linked implementations of
operator new and custom allocators conform to the C++ standard and
throw on allocation failure. By default in Visual Studio, the compiler
pessimistically generates null checks (/Zc:throwingNew-) for these
calls, because users can link with a non-throwing implementation of
operator new or write custom allocator routines that return null
pointers."

Nothing in TGUI assumes that `new` returns nullptr on failure and if
any users write an allocator that does so or explicitly link to a
non-conforming, non-throwing allocator, then they are advanced enough
to know to locally disable this option. We should not penalize the
common user to cater to an obscure corner case (and this also better
matches behaviour with other compilers). Let's default to the standard
compliant, and better performing, behaviour.
@jjuhl jjuhl closed this Jun 15, 2026
@jjuhl

jjuhl commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Need to check for clang. Never mind for now. I'll get back to this at some point in the future.

@texus

texus commented Jun 20, 2026

Copy link
Copy Markdown
Owner

I've added a section for MSVC-specific compiler flags now.
It's best to keep the MSVC and Clang-CL flags the same were possible, but in edge cases like this you can now specify it separately.

Clang-CL already assumes the new implementation is conform, so it doesn't have the option.

This has been committed in b462ae8 and 2c23d2a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants