使用加速融合获取结构名称
getting the structure name with boost fusion
使用boost::fusion
可以迭代一个适应的结构并获取该结构的每个成员的名称。
有没有办法以某种方式检索结构的名称?
我想做的是:鉴于当前的情况
namespace inner {
struct test_struct {
int a;
int b;
} }
BOOST_FUSION_ADAPT_STRUCT( inner::test_struct, a, b );
我想要一个返回"test_struct"(或"inner::test_struct"(的函数
我确实检查了包含struct_size
和其他扩展类的头文件[*],但我还没有找到任何用于此目的的文件。
你知道有什么可以做的吗?
[*] 增强/融合/适应/结构/细节/扩展.hpp
这不是一个功能。您可以通过检查预处理器的输出来查看它。
您可以看到struct_member_name
扩展名,但没有结构名称的此类文字:
namespace boost {
namespace fusion {
namespace traits {
template <> struct tag_of<inner::test_struct> { typedef struct_tag type; };
template <> struct tag_of<inner::test_struct const> {
typedef struct_tag type;
};
} // namespace traits
namespace extension {
template <> struct access::struct_member<inner::test_struct, 0> {
struct deduced_attr_type {
static const inner::test_struct& obj;
typedef boost::type_of::remove_cv_ref_t<decltype(obj.a)> type;
};
typedef deduced_attr_type::type attribute_type;
typedef attribute_type type;
template <typename Seq> struct apply {
typedef typename add_reference<
typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
mpl::identity<attribute_type>>::type>::type
type;
constexpr static type call(Seq& seq) { return seq.a; }
};
};
template <> struct struct_member_name<inner::test_struct, 0> {
typedef char const* type;
constexpr static type call() { return "a"; }
};
template <> struct access::struct_member<inner::test_struct, 1> {
struct deduced_attr_type {
static const inner::test_struct& obj;
typedef boost::type_of::remove_cv_ref_t<decltype(obj.b)> type;
};
typedef deduced_attr_type::type attribute_type;
typedef attribute_type type;
template <typename Seq> struct apply {
typedef typename add_reference<
typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
mpl::identity<attribute_type>>::type>::type
type;
constexpr static type call(Seq& seq) { return seq.b; }
};
};
template <> struct struct_member_name<inner::test_struct, 1> {
typedef char const* type;
constexpr static type call() { return "b"; }
};
template <> struct struct_size<inner::test_struct> : mpl::int_<2> {};
template <> struct struct_is_view<inner::test_struct> : mpl::false_ {};
} // namespace extension
} // namespace fusion
namespace mpl {
template <typename> struct sequence_tag;
template <> struct sequence_tag<inner::test_struct> {
typedef fusion::fusion_sequence_tag type;
};
template <> struct sequence_tag<inner::test_struct const> {
typedef fusion::fusion_sequence_tag type;
};
} // namespace mpl
} // namespace boost
与往常一样,添加自己的宏以获取额外信息可能不太难。例如,参见使用Boost Spirit X3或Boost融合序列类型和结构和类的名称标识使用交替标记解析选择器结构
相关文章:
- 从字符串中获取结构的大小
- 如何在结构初始化中获取成员C++
- 从 cin 获取 c++ 中结构中多个枚举的输入
- 如何通过向量中的结构 id 获取索引号
- 获取结构 c++ 中元素的索引
- 如何获取结构C++的输入
- 使用加速融合获取结构名称
- 如何使用值获取结构的索引
- 如何获取结构体成员的地址
- 结构化绑定,无需复制即可获取子向量的连续元素
- 在通过 P/Invoke 获取的 C++ 结构上设置 C# 回调
- 获取指向映射内结构的指针
- 从行分析中获取结构
- 在模板实例化期间获取原始结构/类名C++
- 如何从 GIFLIB 保存图像结构中获取 RGB 颜色数据
- 获取结构的 UUID
- 如何直接从 FILETIME 结构获取/设置和获取秒数
- 从知道其结构类型的实例中获取值
- 如何使用结构C++获取从函数中生成的最大值
- 从接口层次结构获取接口