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

            Bug ID: 125900
           Summary: A splice expression in a static_cast expression within
                    a template-for statement causes an unexpected error:
                    "consteval-only expressions are only allowed in a
                    constant-evaluated context"
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nishuangcheng at gmail dot com
  Target Milestone: ---

### Environment
- Compiler: GCC 16/17
- Flags: -std=c++26 -freflection

### Description
A splice expression in a `static_cast` expression within a `template for`
statement causes an unexpected error: "consteval-only expressions are only
allowed in a constant-evaluated context".

If a splice expression is used outside a `template for` statement or directly
within a `template for` statement, the code compiles correctly.

Besides, the experimental reflection branch of Clang accepts all the cases.

See the [example](https://godbolt.org/z/1GsfT4Gxq) below.
```cpp
#include <meta>
#include <print>

struct S {
    float f;
};

int main() {
    S s{.f = 1.5f};

    constexpr auto member{^^S::f};
    std::println("{}", s.[:member:]);                    // ok
    std::println("{}", static_cast<int>(s.[:member:]));  // ok

    constexpr auto access_context{std::meta::access_context::current()};
    template for (constexpr auto member :
                  std::define_static_array(nonstatic_data_members_of(^^S,
access_context))) {
        std::println("{}", s.[:member:]);                    // ok
        std::println("{}", static_cast<int>(s.[:member:]));  // error with GCC,
ok with Clang
    }
}
```
  • [Bug c++/125900] New: A splice... nishuangcheng at gmail dot com via Gcc-bugs

Reply via email to