Reflection for C++26
SG16 Telecon — April 24, 2024
#include <experimental/meta>
#include <iostream>
enum MyEnum { x, y, e = -1, f, z = 99 };
struct S {
int key:24;
int flags:8;
double value;
} s = { 100, 0x0d, 42.0 };
int main() {
constexpr std::meta::info refl = ^MyEnum;� std::cout << name_of(refl) << '\n';
std::cout << name_of(enumerators_of(refl)[1]) << '\n';
std::cout << [:enumerators_of(refl)[4]:] << '\n';
std::cout << name_of(nonstatic_data_members_of(^S)[1]) << '\n';
std::cout << s.[:nonstatic_data_members_of(^S)[1]:] << '\n';
}
std::meta::info
^
[: … :]
Constant evaluation guarantees
Metafunctions (lots!)
Core language details (ODR, …)
Source identifiers as string-like arguments and return values.
API elements that produce text
namespace std::meta {
consteval str_type name_of(info);
consteval str_type qualified_name_of(info);
consteval str_type display_name_of(info);
}
API elements that consume text (as of P2996R2)
namespace std::meta {
struct data_member_options_t {
optional<string_view> name;
bool is_static = false;
optional<int> alignment;
optional<int> width;
};
consteval info data_member_spec(info type,
data_member_options_t options = {});
consteval info define_class(info class_type, span<info const>);
}
Constraints
Tension
Proposal sketch #1 (Daveed’s preferred)
�namespace std::meta {� template<char_or_char8_t CharT = char> consteval� std::basic_string_view<CharT> name_of(info);�}
Proposal sketch #2
Proposal sketch #3 (Victor’s idea)