【发布时间】:2018-02-04 11:35:00
【问题描述】:
我正在使用 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()
更具体地说,问题在于 gcc 特有的 typeof()。以下是我尝试过的一些事情:
// 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<> 专门用于引用而不是类型本身。
【问题讨论】:
标签: c++ c++11 boost reflection decltype