C++ Logo

std-proposals

Advanced search

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

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Sat, 26 Apr 2025 19:59:12 -0400
On Sat, Apr 26, 2025 at 7:17 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> The whole 'noexcept' thing has gotten a bit hairy in C++. We've
> already thrown 'throw' specifiers out the window, but even now in
> C++26 the situation isn't ideal, given that people are now writing
> code as follows:
>
> template<typename T>
> bool Func(T &arg) noexcept ( noexcept(++arg) && noexcept((bool)!!arg) )
> {
> ++arg;
> return !!arg;
> }
>
> . . .which is why some people are now asking for:
>
> template<typename T>
> bool Func(T &arg) noexcept(auto)
> {
> ++arg;
> return !!arg;
> }
>
> But anyway let's bring it back to basics for a minute. Consider the
> following program:
>
> int Func1(int const arg)
> {
> throw -1;
> }
>
> int Func2(int const arg) noexcept
> {
> return Func1(arg);
> }
>
> int main(int const argc, char **const argv)
> {
> Func2(42);
> }
>
> I don't know about the rest of you, but I wouldn't want this program
> to compile successfully. I would like a compiler error for the line
> "Func1(arg)" because you're calling a "noexcept(false)" function from
> a "noexcept(true)" function.

No. There is nothing fundamentally wrong with calling a function that
*might* throw exceptions from one that won't.

You have no idea what could cause the possibly throwing function to
throw. Maybe there are certain parameters that would cause it to throw
(nullptr, index-out-of-bounds, etc), but the non-throwing function
already did the check and no throwing can ever happen.

Unless you *know* for certain that this is not the case, you have no
right to decide for other people that this code is ill-formed.

> I fed my idea in ChatGPT

... why do you keep doing things like that? Do you honestly think this
makes your idea seem more legitimate?

Because it *really* doesn't.

"I used ChatGPT for some of this" is basically shorthand for "this is
not a serious proposal."

Received on 2025-04-26 23:59:25