C++ Logo

std-proposals

Advanced search

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

From: David Brown <david.brown_at_[hidden]>
Date: Fri, 22 May 2026 09:03:00 +0200
On 21/05/2026 22:22, Alejandro Colomar via Std-Proposals wrote:
> Hi Thiago,
>
> On 2026-05-21T09:02:40-0700, Thiago Macieira via Std-Proposals wrote:
>> On Thursday, 21 May 2026 08:58:29 Pacific Daylight Time Andrey Semashev via
>> Std-Proposals wrote:
>>>> This happens more commonly with switches over full enumerations, without
>>>>
>>>> default:
>>>> switch (e) {
>>>> case E::A: return 1;
>>>> case E::B: return -1;
>>>> }
>>>> Q_UNREACHABLE_RETURN(0);
>>>
>>> This still doesn't necessitate the templated std::unreachable, the
>>> non-template one would suffice.
>>
>> If the compilers don't get fixed but we did get the templated one, I'd expect
>> the code to get rewritten as:
>>
>> return std::unreachable<int>();
>
> 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.

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