[Bug c++/61372] Add warning to detect noexcept functions that might throw

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Aug 18 14:46:08 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61372

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to David Crocker from comment #3)
> (In reply to Jonathan Wakely from comment #2)
> > extern "C" functions can throw, so it would be wrong to unconditionally
> > assume they can't.
> 
> True, you can write an extern "C" function that throws. But does it ever
> make sense to? I don't think so. Functions written in C and then declared
> extern "C" in a C++ program so that they can be called from C++ won't throw
> or propagate exceptions,

That's not true. std::bsearch and std::qsort are counterexamples. You don't
know what the user-provided function does, and you can't assume it doesn't
throw.

> even if they end up calling functions written in
> C++ that throw. The only reason to write a function in C++ and declare it
> extern "C" is so that it call be called from C, in which case it had better
> not throw.

That's an incorrect assumption. Not all extern "C" functions are written in
C++. Even if they are, you can't assume they don't throw because they could use
callbacks that can throw, or they might throw even in C programs (which works
fine for some targets, unwinding the stack exactly as a C++ program wants it
to).

It would be OK to optionally assume functions with C language linkage don't
throw, but it must be optional.


More information about the Gcc-bugs mailing list