user5473227
user5473227

Reputation:

Enabling warnings when a noexcept function attempts to call non-noexcept function in gcc or clang

Is there a flag in either GCC or Clang that will throw compile time errors (or warnings) when a function marked as noexcept attempts to call a function not marked as noexcept ?

If no, then how are you supposed to tell which parts of your code are affected when you remove noexcept from a previously noexcept -marked funciton ? is there simply no way?

Upvotes: 5

Views: 1357

Answers (1)

JVApen
JVApen

Reputation: 11317

Marking a function noexcept can make sense even when the called function ain't marked. Take a simple example like square root, which might throw if you pass a negative number. When using it in a function that ensures only calls with positive numbers, you can mark it. Same holds if you catch the exception.

That said, having a tool to flag suspicious calls make sense. I am aware that clang has a compiler warning for throwing out of a noexcept function (direct). (See https://clang.llvm.org/docs/DiagnosticsReference.html#wexceptions) For my custom assert macro that works in constexpr I've suppressed it, MSVC does as well.

For the indirect case, aka calling a non marked function, I have yet to see a compiler warning. I do know clangd reports this, most likely because of clang-tidy. A quick check on it's page leads me to believe it's following check: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-escape.html

I can't tell for GCC as I don't use it enough.

Upvotes: 3

Related Questions