boostorg / describe

A C++14 reflection library
https://boost.org/libs/describe
67 stars 24 forks source link

How to use name of the member with Boost Hana? #31

Closed denzor200 closed 2 years ago

denzor200 commented 2 years ago
#include <boost/describe.hpp>
#include <boost/mp11.hpp>
#include <boost/hana.hpp>
#include <cstdio>

enum E {
    v1 = 11,
    v2,
    v3 = 5
};
BOOST_DESCRIBE_ENUM(E, v1, v2, v3)

int main()
{
    constexpr auto D = boost::mp11::mp_front< boost::describe::describe_enumerators<E> >();
    std::printf( "%s: %d\n", D.name, D.value );
}

Is there a way to convert value of D.name to boost::hana::string? https://www.boost.org/doc/libs/1_79_0/libs/hana/doc/html/structboost_1_1hana_1_1string.html If not, is it planned to introduce a similar feature on the Boost Describe side?

pdimov commented 2 years ago

Like this:

namespace hana = boost::hana;

template<class D, std::size_t... I> auto to_hana_string_impl( std::index_sequence<I...> )
{
    return hana::string_c< D::name[I]... >;
}

template<class D> auto to_hana_string()
{
    return to_hana_string_impl<D>( std::make_index_sequence<__builtin_strlen(D::name)>() );
}

(https://godbolt.org/z/G5c51er4W)

It's a bit more complex than it needs to be, but see https://github.com/boostorg/describe/pull/10 for discussion why this is so.

denzor200 commented 2 years ago

Thanks for the detailed answer!