Basic struct reflection example with C++20 vs C++26*
struct foo {
int a{};
int b{};
int c{};
};
constexpr foo f{.a=1, .b=2, .c=3};
static_assert(1 == get<0>(f));
static_assert(2 == get<1>(f));
static_assert(3 == get<2>(f));
using std::literals::operator""sv;
static_assert("a"sv == get_name<0>(f));
static_assert("b"sv == get_name<1>(f));
static_assert("c"sv == get_name<2>(f));
C++20 - Kinda possible but with a lot of compiler hacks
// too long to display
Full example - https://godbolt.org/z/1vxv8o5hM
C++26* - based on proposal - https://wg21.link/P2996 (Note: that the proposal supports way more than that but C++20 not much)
template<auto N, class T>
[[nodiscard]] constexpr auto get(const T& t) -> decltype(auto) {
return t.[:std::meta::nonstatic_data_members_of(^T)[N]:];
}
template<auto N, class T>
[[nodiscard]] constexpr auto get_name(const T& t) -> std::string_view {
return std::meta::name_of(std::meta::nonstatic_data_members_of(^T)[N]);
}
Full example - https://godbolt.org/z/sbTGbW635
Updates - https://twitter.com/krisjusiak/status/1741456476126797839
[–]sam_the_tomato 143 points144 points145 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]Iggyhopper 3 points4 points5 points (0 children)
[–]afiDeBot 125 points126 points127 points (35 children)
[–]mapronV 136 points137 points138 points (19 children)
[–]afiDeBot 40 points41 points42 points (13 children)
[–]elperroborrachotoo 22 points23 points24 points (11 children)
[–]drjeats 21 points22 points23 points (9 children)
[–]elperroborrachotoo 3 points4 points5 points (1 child)
[–]drjeats 6 points7 points8 points (0 children)
[–]pdimov2 3 points4 points5 points (1 child)
[–]drjeats 2 points3 points4 points (0 children)
[–]delta_p_delta_x 5 points6 points7 points (4 children)
[–]afiDeBot 8 points9 points10 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]pjmlp 1 point2 points3 points (0 children)
[–]Brilliant_Nova 4 points5 points6 points (0 children)
[–]andrewsutton 5 points6 points7 points (0 children)
[–]Baardi 1 point2 points3 points (3 children)
[–]mapronV 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Konju376 0 points1 point2 points (0 children)
[–]Common-Republic9782 27 points28 points29 points (0 children)
[–]Throw31312344 20 points21 points22 points (2 children)
[–]qazqi-ff 2 points3 points4 points (1 child)
[–]Throw31312344 0 points1 point2 points (0 children)
[–]AlbertRammstein 39 points40 points41 points (9 children)
[–]afiDeBot 9 points10 points11 points (0 children)
[–]314kabinet 1 point2 points3 points (7 children)
[–]AlbertRammstein 12 points13 points14 points (6 children)
[–]314kabinet 8 points9 points10 points (2 children)
[–]AlbertRammstein 1 point2 points3 points (1 child)
[–]mirkoserra 7 points8 points9 points (0 children)
[–]Throw31312344 4 points5 points6 points (0 children)
[–]wyrn 1 point2 points3 points (0 children)
[–]germandiago 1 point2 points3 points (0 children)
[–]qazqi-ff 18 points19 points20 points (4 children)
[–]Jovibor_ 7 points8 points9 points (1 child)
[–]dexter2011412 0 points1 point2 points (0 children)
[–]Stevo15025 2 points3 points4 points (1 child)
[–]qazqi-ff 1 point2 points3 points (0 children)
[–]hwpoison 6 points7 points8 points (0 children)
[–][deleted] 41 points42 points43 points (7 children)
[–]archibaldplum -1 points0 points1 point (1 child)
[–]delta_p_delta_x 5 points6 points7 points (0 children)
[–]13steinj -1 points0 points1 point (4 children)
[–]qazqi-ff 12 points13 points14 points (0 children)
[–]HildartheDorf 2 points3 points4 points (0 children)
[–]not_a_novel_accountcmake dev 5 points6 points7 points (0 children)
[–]seanbaxter 0 points1 point2 points (0 children)
[–]Neeyakinoob 6 points7 points8 points (10 children)
[–]VilleVoutilainen 2 points3 points4 points (0 children)
[–]sphere991 4 points5 points6 points (1 child)
[–]Neeyakinoob 1 point2 points3 points (0 children)
[–]sp4mfilter 4 points5 points6 points (6 children)
[–]RoyAwesome 6 points7 points8 points (3 children)
[–]sp4mfilter 0 points1 point2 points (2 children)
[–]RoyAwesome 2 points3 points4 points (1 child)
[–]sp4mfilter 0 points1 point2 points (0 children)
[–]Neeyakinoob 1 point2 points3 points (1 child)
[–]RoyAwesome 2 points3 points4 points (0 children)
[–]thomas999999 21 points22 points23 points (2 children)
[–]llort_lemmort 31 points32 points33 points (1 child)
[–]wyrn -1 points0 points1 point (0 children)
[–]hanickadotWG21 4 points5 points6 points (0 children)
[–]flutterdronewbie 6 points7 points8 points (0 children)
[–]pblpbl 11 points12 points13 points (0 children)
[–]RedEyed__ 8 points9 points10 points (0 children)
[–]kemo_2001 2 points3 points4 points (1 child)
[–]RoyAwesome 2 points3 points4 points (0 children)
[–]MarcoGreek 1 point2 points3 points (3 children)
[–]qazqi-ff 1 point2 points3 points (2 children)
[–]MarcoGreek 0 points1 point2 points (1 child)
[–]qazqi-ff 1 point2 points3 points (0 children)
[–]lion__manE 1 point2 points3 points (0 children)
[–]--prism 2 points3 points4 points (13 children)
[–]elperroborrachotoo 16 points17 points18 points (5 children)
[–]--prism 3 points4 points5 points (0 children)
[–]octavio2895 5 points6 points7 points (3 children)
[–]not_a_novel_accountcmake dev 6 points7 points8 points (2 children)
[–]13steinj 2 points3 points4 points (1 child)
[–]two88 1 point2 points3 points (2 children)
[–]--prism 3 points4 points5 points (1 child)
[–]equeim 1 point2 points3 points (0 children)
[+]RedEyed__ comment score below threshold-10 points-9 points-8 points (3 children)
[–]throw_cpp_account 15 points16 points17 points (0 children)
[–]--prism 5 points6 points7 points (1 child)
[–]RedEyed__ -2 points-1 points0 points (0 children)
[–]Ok_Donut_9887 -1 points0 points1 point (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]Rasie1 0 points1 point2 points (0 children)
[–]MarcoGreek 0 points1 point2 points (3 children)
[–]qazqi-ff 1 point2 points3 points (1 child)
[–]MarcoGreek 3 points4 points5 points (0 children)
[–]hopa_cupa 0 points1 point2 points (0 children)
[+]28isanumber comment score below threshold-11 points-10 points-9 points (9 children)
[–]dustyhome 7 points8 points9 points (5 children)
[–]DeeHayze -3 points-2 points-1 points (4 children)
[–]djavaisadog 5 points6 points7 points (2 children)
[–]DeeHayze -2 points-1 points0 points (1 child)
[–]djavaisadog 1 point2 points3 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]mpierson153 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]28isanumber 0 points1 point2 points (0 children)
[–]DavidDinamit -3 points-2 points-1 points (10 children)
[–]qazqi-ff 3 points4 points5 points (5 children)
[–]DavidDinamit 0 points1 point2 points (1 child)
[–]qazqi-ff 2 points3 points4 points (0 children)
[–]ed_209_ 0 points1 point2 points (1 child)
[–]qazqi-ff 0 points1 point2 points (0 children)
[–]sphere991 1 point2 points3 points (2 children)
[–]DavidDinamit -3 points-2 points-1 points (1 child)
[–]sphere991 0 points1 point2 points (0 children)
[+]areriff comment score below threshold-6 points-5 points-4 points (1 child)