C++ Logo

std-proposals

Advanced search

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

From: Mital Ashok <mital_at_[hidden]>
Date: Thu, 21 May 2026 13:32:53 +0100
This should be a non-breaking extension.
Add a templated function:

    template<class T>
    [[noreturn]] T unreachable() {
        unreachable();
    }

This is useful in ternary conditional expressions, like `o.is_valid()
? *o : std::unreachable<O::value_type>()`.

It can also be used in some weird cases where you want to ODR-use
std::declval in a constant expression (e.g., when used as an unknown
reference)

T is returned instead of add_rvalue_reference_t<T> intentionally
because of the use case in conditional expressions where we don't want
to materialise a temporary the other side (so std::unreachable<int>()
is a prvalue for example). This deviates from std::declval but has
similar semantics to std::reference_con{struct,vert}s_from_temporary
(T is prvalue, T& is lvalue, T&& is xvalue), with the unfortunate
exclusion of array types, but that should not be a big use case.

The above is one of the things that can be discussed about this
facility. I was mostly wondering if there is anything else I need to
consider before writing R0 of the proposal

Received on 2026-05-21 12:33:32