Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
[C++20] to_tuple with names (twitter.com/krisjusiak)
2 points by kris-jusiak on Dec 5, 2023 | hide | past | favorite | 1 comment


Following on 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: