C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::unreachable<T>()

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Fri, 22 May 2026 09:44:57 +0200
Hi David,

On 2026-05-22T09:03:00+0200, David Brown wrote:
> > I expect that a compiler should be able to recognize that a function
> > ending in
> >
> > ...
> > unreachable();
> > }
> >
> > does not need a return statement. If a compiler can't fix that , I don't
> > expect they'll implement a fancy templated unreachable.
> >
>
> The fancy template can perhaps be implemented just in the implementer's
> library, as much the same as std::declval<>() but without the checks for
> unevaluated contexts and with a call to std::unreachable() added. That is
> probably easier to do than improving std::unreachable() in terms of
> optimisation and its effect on compiler warnings, all of which need work in
> the compiler itself.

There's no need to improve anything about optimizations. It's as simple
as letting the compiler think that 'unreachable()' is a type of 'return'
statement. That is, whenever the compiler sees 'unreachable()', it
should --for the exclusive purposes of the diagnostic-- consider as if
it had seen a return statement.

That is, the function below should have no diagnostics about missing
a return statement in a non-void function. (But it should probably
have other diagnostics.)

 int f(void)
 {
  unreachable();
 }


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>

Received on 2026-05-22 07:45:09