https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124558
Bug ID: 124558
Summary: [reflection] annotations_of[_with_type] relies on new
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: septicake056 at gmail dot com
Target Milestone: ---
Attempting to use either of `std::meta::annotations_of` or
`std::meta::annotations_of_with_type` in a constant expression context results
in an error, as it seems to internally rely on allocation.
```
#include <meta>
struct s{};
class [[=s{}]] c {
public:
[[=s{}]]
void f(){}
[[=s{}]]
int i;
class [[=s{}]] c1 {};
};
int main() {
using namespace std::meta;
template for(constexpr info m : std::meta::annotations_of(^^c::f)) {
// fails
}
template for(constexpr info m : annotations_of_with_type(^^c::i, ^^s)) {
// fails
}
template for(constexpr info m : annotations_of(^^c::c1)) {
// fails
}
template for(constexpr info m : annotations_of_with_type(^^c, ^^s)) {
// fails
}
}
```
This bug prevents usage in `template for` bodies such as in the examples shown
in paper 3394.
Compiler info:
```
Using built-in specs.
COLLECT_GCC=./gcc-16/bin/g++
COLLECT_LTO_WRAPPER=/home/authed-user/gcc-16/libexec/gcc/x86_64-pc-linux-gnu/16.0.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure --prefix=/home/authed-user/gcc-16
--enable-languages=c,c++ --disable-multilib --disable-bootstrap --disable-nls
--with-system-zlib --enable-checking=release
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.0.1 20260317 (experimental) (GCC)
```
CLI Options: `-v -std=c++26 -freflection`
Compiler Output:
```
test.cpp: In function 'int main()':
test.cpp:21:5: error: 'std::meta::annotations_of(^^c::f)' is not a constant
expression because it refers to a result of 'operator new'
21 | }
| ^
In file included from /home/___/gcc-16/include/c++/16.0.1/string:46,
from
/home/___/gcc-16/include/c++/16.0.1/bits/stdexcept_throw.h:57,
from /home/___/gcc-16/include/c++/16.0.1/array:44,
from /home/___/gcc-16/include/c++/16.0.1/meta:42,
from test.cpp:1:
/home/___/gcc-16/include/c++/16.0.1/bits/allocator.h:203:52: note: allocated
here
203 | return static_cast<_Tp*>(::operator new(__n));
| ~~~~~~~~~~~~~~^~~~~
test.cpp:24:5: error: 'std::meta::annotations_of_with_type(^^c::i, ^^s)' is not
a constant expression because it refers to a result of 'operator new'
24 | }
| ^
/home/___/gcc-16/include/c++/16.0.1/bits/allocator.h:203:52: note: allocated
here
203 | return static_cast<_Tp*>(::operator new(__n));
| ~~~~~~~~~~~~~~^~~~~
test.cpp:27:5: error: 'std::meta::annotations_of(^^c::c1)' is not a constant
expression because it refers to a result of 'operator new'
27 | }
| ^
/home/___/gcc-16/include/c++/16.0.1/bits/allocator.h:203:52: note: allocated
here
203 | return static_cast<_Tp*>(::operator new(__n));
| ~~~~~~~~~~~~~~^~~~~
test.cpp:30:5: error: 'std::meta::annotations_of_with_type(^^c, ^^s)' is not a
constant expression because it refers to a result of 'operator new'
30 | }
| ^
/home/___/gcc-16/include/c++/16.0.1/bits/allocator.h:203:52: note: allocated
here
203 | return static_cast<_Tp*>(::operator new(__n));
| ~~~~~~~~~~~~~~^~~~~