C++ Logo

std-proposals

Advanced search

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

From: Muhammad <mail_at_[hidden]>
Date: Sun, 26 Oct 2025 19:43:54 +0000
Allow consteval functions to return types by using the declarator
typename auto. These functions would be evaluated at compile time and
usable anywhere a type is expected.

Motivation

Express compile-time type computation using normal control flow.

Zero runtime cost since consteval are deduced at compile time.

Rules (proposal)

Must be declared consteval.

Each return must produce a type expression (e.g. int, std::vector<T>,
decltype(expr)).

Usable in using, declarations, template arguments, concepts, etc.

Why

More readable than alias templates/struct metafunctions for many cases.

Aligns type-level programming with existing constexpr/consteval function
model


Current syntax:

template<int N>
struct select_type {
     using type = std::conditional_t<N == 0, int, double>;
};

using T = typename select_type<1>::type; // T == double

Proposed syntax:

consteval typename auto select_type(int n) {
     return n == 0 ? int : double;
}

or using the trailing type syntax

consteval auto select_type(int n) -> typename auto {
     return n == 0 ? int : double;
}

using T = select_type(1); // T == double

More examples

template<typename T>
consteval typename auto make_pointer_if(bool b) {
     return b ? T* : T;
}

using A = make_pointer_if<int>(true); // int*
using B = make_pointer_if<int>(false); // int

tell me what you think.

Received on 2025-10-26 19:44:01