C++ Logo

std-proposals

Advanced search

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

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 21 May 2026 08:34:43 -0700
On Thursday, 21 May 2026 06:02:17 Pacific Daylight Time Andrey Semashev via
Std-Proposals wrote:
> > This is useful in ternary conditional expressions, like `o.is_valid()
> > ? *o : std::unreachable<O::value_type>()`.
>
> How is this different from just `*o`?

The only place I see this being used is the equivalent of Qt macro
QT_UNREACHABLE_RETURN(), which expands to:

  do { Q_UNREACHABLE(); return __VA_ARGS__; } while (0)

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);

The way I personally solve this is to select one of the enumerations and make
it a break; which then gets the full return. But this is removing a tiny bit
of information from the compiler: the code above says the only valid values
are those in the enumeration; with the break, all values are permitted.

But that only exists *because* some compilers complain that you didn't return
a value from a non-void function after the unreachable marker... because they
don't understand the marker in the first place. So from the point of view of
usability, I'd prefer the compiler get fixed.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.

Received on 2026-05-21 15:34:50