I'm reflecting structures using boost::hana and I'm trying to get the name of members. My working solution uses a gcc-specific macro and I'd like to replace it with something more generic.
The header which provides the reflection looks a little like this
#include <boost/hana.hpp>
#include <string>
#include <iostream>
struct ReflectedStruct
{
virtual void PrintMemberTypes() = 0;
virtual ~ReflectedStruct(){}
};
template<class T> struct meta {
static const std::string& get_name() {return T::class_name;}
};
#define TYPE_NAME(type) \
template<>struct meta<type> { \
static constexpr const char* class_name = #type; \
static const char* get_name() {return class_name;} \
};
TYPE_NAME(double);
#define REFLECT_STRUCT(structure_name, ...) \
struct structure_name : ReflectedStruct { \
BOOST_HANA_DEFINE_STRUCT(structure_name, __VA_ARGS__ ); \
void PrintMemberTypes() { \
boost::hana::for_each(boost::hana::accessors<structure_name>(), \
[&](auto pair) { \
std::cout \
<< meta< typeof( boost::hana::second(pair)(*this) ) >::get_name() \
<< std::endl; \
} \
); \
} \
}; \
TYPE_NAME(structure_name);
And using it looks like this:
REFLECT_STRUCT(Runway,
(double, lat),
(double, lon),
(double, hdg),
(double, alt)
);
int main()
{
Runway r;
r.PrintMemberTypes(); // prints "double, double, double, double"
return 0;
}
My problem is the line which looks like this:
meta< typeof( boost::hana::second(pair)(*this) ) >::get_name()
More specifically, the problem is the typeof()
which is gcc-specific. Here are some things I've tried:
// Ok, but gcc-specific. Resolves to "double"
typeof ( boost::hana::second(pair)(*this) )
// Error, is type "double&", I haven't specialized meta<> for that.
decltype ( boost::hana::second(pair)(*this) )
// error: Doesn't seem to resolve to a type
std::remove_reference<decltype( boost::hana::second(pair)(*this) )>::type
// error: Doesn't seem to resolve to a type
boost::hana::typeid_(boost::hana::second(pair)(*this))::type
I need the type to be without the reference. I guess the other option would be to specialize meta<>
for the references instead of the types themselves.
Aucun commentaire:
Enregistrer un commentaire