[Bug c++/121594] Expansion statement with constexpr for-range-declarator does not compile
boris.staletic at protonmail dot com
gcc-bugzilla@gcc.gnu.org
Wed Aug 20 05:51:23 GMT 2025
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121594
--- Comment #3 from Boris Staletic <boris.staletic at protonmail dot com> ---
> Please next time attach or place inline the testcases rather than just linking against godbolt.
Noted.
I managed to reproduce these compile time errors without any standard library
headers:
```
struct array { int arr[3]; };
struct view {
const char* p;
unsigned long long size;
constexpr view(const char* str) : p(str), size(3) {}
};
struct T {
int mem1;
char mem2;
view mem3;
constexpr T(int a, char b, view c) : mem1(a), mem2(b), mem3(c) {}
};
template<unsigned long long I>
constexpr auto get(T t) {
if constexpr (I == 0) return t.mem1;
else if constexpr (I == 1) return t.mem2;
else return t.mem3;
}
namespace std {
template<typename T> struct tuple_size;
template<> struct tuple_size<T> { static constexpr unsigned long long value =
3; };
template<unsigned long long I, typename T> struct tuple_element;
template<> struct tuple_element<0, T> { using type = int; };
template<> struct tuple_element<1, T> { using type = char; };
template<> struct tuple_element<2, T> { using type = view; };
} // namespace std
int main() {
template for (constexpr auto I : array{1, 2, 3}) { /* ... */ }
template for (constexpr auto I : T{1, '2', "asdasd"}) { /* ... */ }
}
```
The errors look like this:
```
<source>: In function 'int main()':
<source>:31:38: error: '<anonymous>' is not a constant expression
31 | template for (constexpr auto I : array{1, 2, 3}) { /* ... */ }
| ^~~~~~~~~~~~~~
<source>:32:71: error: the value of '<anonymous>' is not usable in a constant
expression
32 | template for (constexpr auto I : T{1, '2', "asdasd"}) { /* ... */ }
| ^
<source>:32:71: note: '<anonymous>' was not declared 'constexpr'
<source>:32:71: error: modification of '<anonymous>' from outside current
evaluation is not a constant expression
```
More information about the Gcc-bugs
mailing list