Is this possible? I don't think it is, but I don't know if this is something the standard would say, or if it's implementation defined? I'm asking because I'm wondering whether it's safe or worth it to mark a constexpr function like this noexcept
EX:
constexpr double to_meters(double y) noexcept? {
return y * 10;
}
constexpr double x = to_meters(y); // Clang-Tidy warns about possible exception without noexcept
No, float point multiplication will normally not throw a C++ exception.
But think about this: How can clang-tidy possibly know whether to_meter
will throw an exception? In C++ every function can throw an exception unless it is explicitly declared not to throw.
So clang-tidy has two options: It could do expensive (possibly inconclusive) control flow analysis or it can simply rely on you correctly declaring nothrow
, which it does:
Finder->addMatcher(
varDecl(anyOf(hasThreadStorageDuration(), hasStaticStorageDuration()),
unless(hasAncestor(functionDecl())),
anyOf(hasDescendant(cxxConstructExpr(hasDeclaration(
cxxConstructorDecl(unless(isNoThrow())).bind("func")))),
//^^^^^^^^^^^^^^^^^^^
hasDescendant(cxxNewExpr(hasDeclaration(
functionDecl(unless(isNoThrow())).bind("func")))),
//^^^^^^^^^^^^^^^^^^^
hasDescendant(callExpr(hasDeclaration(
functionDecl(unless(isNoThrow())).bind("func"))))))
.bind("var"),
this);
The language definition doesn't give you any guarantees here, but since virtually every implementation (that is, there are none I know of that don't) implements IEEE-754 math, which does not throw exceptions, it's not something I'd worry about. And more generally, a floating-point math package that throws exceptions would have had to be written with C++ in mind; that's highly unlikely.
However, you may well get messages when a floating-point error occurs that refer to a "floating-point exception"; that's a floating-point exception, not a C++ exception, and it has nothing to do with C++ exceptions. It's a runtime error, with a peculiar name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With