C++ Logo

std-proposals

Advanced search

Re: [std-proposals] noexcept has gotten a bit hairy -- I want a compiler error

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Sat, 10 May 2025 13:12:49 +0100
On Sat, 10 May 2025, 11:40 Simon Schröder via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> It seems that people have already forgotten: C++ used to have a way to
> mark functions that are not noexcept: ‘throw’. It turned out that
> programmers were now good at providing this information which is why we now
> annotate functions the other way around. Marking functions as throwing was
> so unpopular that this feature was deprecated in C++11 and removed in C++17
> (https://en.cppreference.com/w/cpp/language/except_spec).
>
> Concerning C functions: Would it make sense to automatically declare all
> ‘extern “C”’ functions noexcept?
>


No! Because nothing says an extern "C" function can't throw. All extern "C"
means is that the function uses a different language linkage (in practice,
it disables name mangling, and so prevents overloading the name, and
theoretically uses a different calling convention). It doesn't mean the
function can't use C++ features like exceptions, references, lambda
expressions etc.


Or is this something that is already allowed for compilers to assume?
>

Absolutely not. You can't even assume that extern "C" functions from the C
standard library won't throw. The ones that take a callback (such as qsort)
might throw if the callback throws.


It might be a bad idea if someone provides a C interface for a C++
> implementation, though.
>
>
>

Received on 2025-05-10 12:13:07