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

标签 c++ c++11 boost reflection decltype

我正在使用 boost::hana 反射(reflect)结构,我正在尝试获取成员的名称。我的工作解决方案使用特定于 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()

更具体地说,问题是 typeof()这是特定于 gcc 的。以下是我尝试过的一些方法:

// 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

我需要没有引用的类型。 我想另一个选择是专门化 meta<>用于引用而不是类型本身。

最佳答案

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

由于 pair 的类型基本上是一个模板参数,type 是一个从属名称,因此您需要使用 typename :

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

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

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

关于c++ - Boost hana 获取字符串中的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48607613/

相关文章:

c++ - 关于使用 boost::zip 迭代器的一些代码的问题

c++ - std::function:参数的严格编译时验证

c++ - 为什么我每次使用 mingw gcc4.8.1 运行 std::random_device 都会得到相同的序列?

c++ - GNUMakefile 和 gcc : resolving the order of conflicting "-std=c++" requirements

c++ - 比较将整数值转换为字符串的 3 种现代 C++ 方法

c++ - make_pair 如何知道其参数的类型?

c++ - operator<< 和 std::stringstream 引用?

c# - 在 C++ 和 C# 之间共享内存映射文件结构

C++ Boost 反序列化 what() : input stream error

c++ - 使用带有 Boost ASIO 的 UDP 的文件套接字 I/O