Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2020-09-24 22:26:22


> Vinícius dos Santos Oliveira wrore:
> > Hi Peter,
> >
> > could you consider using "compile-time strings" such as Hana's? I don't
> > have much use for a const char* in TMP algorithms.
>
> The descriptor strings are "compile-time". This, for instance, works.
...

Here's how you can get a Hana string:

#include <boost/describe.hpp>
#include <boost/mp11.hpp>
#include <boost/hana.hpp>
#include <cstddef>

using namespace boost::describe;
using namespace boost::mp11;

enum E
{
    v1
};

BOOST_DESCRIBE_ENUM(E, v1)

constexpr std::size_t strlen_( char const * s )
{
    std::size_t n = 0;
    while( *s++ ) ++n;
    return n;
}

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

template<class D> constexpr auto to_string()
{
    return to_string_impl<D>( make_index_sequence<strlen_(D::name)>() );
}

using D = mp_first< describe_enumerators<E> >;

constexpr auto st = to_string<D>();

#include <typeinfo>
#include <cstdio>

int main()
{
    std::printf( "%s\n", typeid(st).name() );
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk