2 ms·
[C++20] to_tuple with names
- kris-jusiak 3y agoFollowing on https://reddit.com/r/cpp/comments/1890jr9/reflectcpp_automatic_field_name_extraction_from/ https://reddit.com/r/cpp/comments/1890jr9/reflectcpp_automat... the following is simple implementation of to_tuple with names (compile-time names) which seems to work on gcc,clang,msvc* It uses extern combined with source_location to get the field name. Example struct foo { int first_field; int second_field; }; constexpr auto t = to_tuple(foo{.first_field = 42, .second_field = 87}); static_assert("first_field"sv == std::get<0>(t).name and 42 == std::get<0>(t).value); static_assert("second_field"sv == std::get<1>(t).name and 87 == std::get<1>(t).value); 100 LOC example - https://godbolt.org/z/sEMPxjGfP https://godbolt.org/z/sEMPxjGfP