5

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?

  • "how are you supposed to tell which parts of your code are affected when you remove noexcept from a previously noexcept -marked funciton ?" - You audit your code very carefully. – Jesper Juhl Jan 10 '20 at 07:48

1 Answers1

3

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.

JVApen
  • 11,008
  • 5
  • 31
  • 67
  • You could use a [`reinterpret_cast()` in order to cast a non-`noexcept` function to a `noexcept` function](https://stackoverflow.com/a/35553018/3779655) after explicitly checking the requirements. This way the (indirect) compiler check would still work. – darkdragon Sep 17 '22 at 16:06
  • Working link to llvm: https://clang.llvm.org/extra/clang-tidy/checks/bugprone/exception-escape.html – PolyGlot Jan 31 '23 at 15:59
  • Thanks @polyglot I've updated the URL. Seems they reshuffled their documentation – JVApen Feb 01 '23 at 05:51