C++ Logo

std-proposals

Advanced search

Re: [std-proposals] consteval typename auto - type-returning immediate functions

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Mon, 27 Oct 2025 12:13:19 +0000
On Sun, 26 Oct 2025 at 22:14, Thiago Macieira via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Sunday, 26 October 2025 12:43:54 Pacific Daylight Time Muhammad via Std-
> Proposals wrote:
> > consteval typename auto select_type(int n) {
> > return n == 0 ? int : double;
> > }
>
> Already possible:
>
> template<int N> constexpr auto select_type()
> {
> if constexpr (n == 0)
> return int{};
> else
> return double{}
> }
>
> using T = decltype(select_type<1>());



This is problematic for arbitrary types, which might not be default
constructible. In generic code, you don't know how to construct a value to
return.

But you can return std::type_identity<int>{} or
std::type_identity<double>{} and then use decltype(select_type<1>())::type.

Received on 2025-10-27 12:13:36