C++ Boost hana获取字符串中的类型

C++ Boost hana获取字符串中的类型,c++,c++11,boost,reflection,decltype,C++,C++11,Boost,Reflection,Decltype,我正在使用boost::hana反映结构,并试图获取成员的名称。我的工作解决方案使用了一个特定于gcc的宏,我想用更通用的宏来代替它 提供反射的标题看起来有点像这样 #include <boost/hana.hpp> #include <string> #include <iostream> struct ReflectedStruct { virtual void PrintMemberTypes() = 0; virtual ~Refle

我正在使用boost::hana反映结构,并试图获取成员的名称。我的工作解决方案使用了一个特定于gcc的宏,我想用更通用的宏来代替它

提供反射的标题看起来有点像这样

#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);
我的问题是这条线看起来像这样:

REFLECT_STRUCT(Runway,
  (double, lat),
  (double, lon),
  (double, hdg),
  (double, alt)
);

int main()
{
    Runway r;
    r.PrintMemberTypes();  // prints "double, double, double, double"
    return 0;
}
meta< typeof( boost::hana::second(pair)(*this) ) >::get_name()
我需要没有引用的类型。 我想另一种选择是专门化引用的
meta
,而不是类型本身

// error: Doesn't seem to resolve to a type
std::remove_reference<decltype( boost::hana::second(pair)(*this) )>::type

您需要记住,
typeid\uu
返回的是一个值,而不是一个类型,因此您不能直接在其结果上使用
::type
。您需要使用
decltype

typename decltype(boost::hana::typeid_(boost::hana::second(pair)(*this)))::type

你说得对。两种解决方案都有效。typename是我错过的最重要的东西。我将坚持使用您的hana::typeid_uu解决方案,因为它可能会更好地处理我尚未想到的任何边缘情况。
typename 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
typename decltype(boost::hana::typeid_(boost::hana::second(pair)(*this)))::type