Using modules and reflection

11 hours ago 1
ARTICLE AD BOX

I am attempting to use C++ modules with reflection. When I #include <meta> things compile and work. Using import std; instead causes compiler errors.

import std; does work so long as I am not using anything in std::meta.

Using readelf, I do not see any symbols in std.gcm from the std::meta namespace.

Is this an issue in gcc 16.1.1, or is there something wrong with my code?

The code:

import std; int main() { constexpr auto ns_refl = ^^std; constexpr auto ctx = std::meta::access_context::unchecked(); constexpr auto members = std::define_static_array(std::meta::members_of(ns_refl, ctx)); template for (constexpr auto &member : auto(members)) { if constexpr (std::meta::has_identifier(member)) std::cout << std::meta::identifier_of(member) << '\n'; } }

Compiler options:

-std=c++26 -O3 -flto

Error:

: In function ‘int main()’: :5:35: error: ‘std::meta::access_context’ has not been declared 5 | constexpr auto ctx = std::meta::access_context::unchecked(); | ^~~~~~~~~~~~~~ :7:12: error: ‘define_static_array’ is not a member of ‘std’ 7 | std::define_static_array(std::meta::members_of(ns_refl, ctx)); | ^~~~~~~~~~~~~~~~~~~ :7:43: error: ‘members_of’ is not a member of ‘std::meta’ 7 | std::define_static_array(std::meta::members_of(ns_refl, ctx)); | ^~~~~~~~~~ :9:30: error: ‘has_identifier’ is not a member of ‘std::meta’ 9 | if constexpr (std::meta::has_identifier(member)) | ^~~~~~~~~~~~~~ :10:31: error: ‘identifier_of’ is not a member of ‘std::meta’ 10 | std::cout << std::meta::identifier_of(member) << '\n'; | ^~~~~~~~~~~~~

A minimal compiling (or failing with the errors I'm seeing...) example can be found here: https://godbolt.org/z/eKhz49z8K

Read Entire Article