Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
denzor200 opened this issue Jul 4, 2022 · 2 comments
Closed

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

denzor200 opened this issue Jul 4, 2022 · 2 comments

Comments

@denzor200
Copy link
Contributor

#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
Copy link
Member

pdimov commented Jul 4, 2022

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 #10 for discussion why this is so.

@denzor200
Copy link
Contributor Author

Thanks for the detailed answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants