https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123691
Bug ID: 123691
Summary: Expansion Statements are implemented but do not work.
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: d7d1cd at mail dot ru
Target Milestone: ---
This document (https://gcc.gnu.org/gcc-16/changes.html#cxx) states that
expansion statements
(https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p1306r5.html) are
implemented for version 16. However, the following code using this feature
doesn't compile:
$ cat source.cc
#include <meta>
#include <print>
enum Enum { ZERO, ONE, TWO };
template <typename E>
requires std::is_enum_v<E>
constexpr auto enum_to_string(E value) {
template for (constexpr auto e : std::meta::enumerators_of(^^E)) {
if (value == [:e:])
return std::meta::display_string_of(e);
}
return std::string_view{"<unnamed>"};
}
int main() {
std::println("{}", enum_to_string(Enum::ONE));
}
$ g++ -std=c++26 -freflection source.cc
<source>: In instantiation of 'constexpr auto enum_to_string(E) [with E =
Enum]':
required from here
<source>:26:38:
26 | std::println("{}", enum_to_string(Enum::ONE));
| ~~~~~~~~~~~~~~^~~~~~~~~~~
<source>:12:5: error: modification of '<temporary>' from outside current
evaluation is not a constant expression
12 | template for (constexpr auto e : std::meta::enumerators_of(^^E))
| ^~~~~~~~
<source>: In function 'int main()':
<source>:26:38: error: call to consteval function
'enum_to_string<Enum>(ONE)' is not a constant expression
26 | std::println("{}", enum_to_string(Enum::ONE));
| ~~~~~~~~~~~~~~^~~~~~~~~~~
<source>:26:38: error: 'constexpr auto enum_to_string(E) [with E = Enum]'
called in a constant expression
<source>:26:38: note: 'constexpr auto enum_to_string(E) [with E = Enum]'
was promoted to an immediate function
Here's the same code in the online compiler: https://godbolt.org/z/EzGT5joWj
Am I using this feature incorrectly, or is there a bug?