https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123828

            Bug ID: 123828
           Summary: [reflection] Yet another unexpected "consteval-only
                    expressions are only allowed in a constant-evaluated
                    context"
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kirshamir at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/b47z4zj3d

Note that the template version of the function works, template argument not
used.

#include <tuple>
#include <meta>
#include <array>

template <std::size_t... Is>
consteval auto to_tuple_impl(const std::ranges::random_access_range auto& v,
std::index_sequence<Is...>) {
    return std::make_tuple(v[Is]...);
}

template <std::size_t N>
consteval auto to_tuple(const std::ranges::random_access_range auto& v) {
    return to_tuple_impl(v, std::make_index_sequence<N>{});
}

constexpr auto types_a() {
    // error: consteval-only expressions are only allowed in a
constant-evaluated context
    return to_tuple<1>(std::array{^^int});
}

// template version: ok
template<typename T>
constexpr auto types_b() {
    return to_tuple<1>(std::array{^^int});
}

int main() {
    // error: consteval-only expressions are only allowed in a
constant-evaluated context
    constexpr auto a = types_a();
    static_assert(std::get<0>(a) == ^^int);

    // ok:
    constexpr auto b = types_b<void>();
    static_assert(std::get<0>(b) == ^^int);
}

Reply via email to