【问题标题】:How to check if a boost fusion sequence is an adapted struct?如何检查升压融合序列是否是适应结构?
【发布时间】:2019-11-16 18:45:26
【问题描述】:
如果一个序列实际上是一个适应的结构,那么在编译时是否有特征或元函数或任何要检查的东西,以便我可以例如得到它的成员名字?我看到人们通过排除来做到这一点,类似于“如果它不是一个向量但它仍然是一个序列,那么它一定是一个结构”(我正在编造,因为我记不太清了)。我不认为这是一个充分条件,可能应该有更好的融合来实现这一点。虽然找不到。知道的请分享。谢谢。
【问题讨论】:
标签:
c++
struct
metaprogramming
boost-fusion
【解决方案1】:
不知道有没有更好的办法,但是你可以使用:
template <typename T>
using is_adapted_struct=std::is_same<typename boost::fusion::traits::tag_of<T>::type,boost::fusion::struct_tag>;
这将适用于使用BOOST_FUSION_ADAPT_STRUCT 调整或使用BOOST_FUSION_DEFINE_STRUCT 定义的结构,我相信它们的命名和模板变体(但不适用于BOOST_FUSION_ADAPT_ASSOC_STRUCT 及其变体(您需要将struct_tag 替换为@ 987654327@ 让它工作))。我认为(这可能是您的用例的一个问题)对于使用 BOOST_FUSION_ADAPT_ADT 改编的类,这也将返回 true。
Example on Wandbox
#include <iostream>
#include <type_traits>
#include <boost/fusion/include/tag_of.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/vector.hpp>
struct adapted
{
int foo;
double bar;
};
BOOST_FUSION_ADAPT_STRUCT(adapted, foo, bar);
struct not_adapted{};
template <typename T>
using is_adapted_struct=std::is_same<typename boost::fusion::traits::tag_of<T>::type,boost::fusion::struct_tag>;
int main()
{
static_assert(is_adapted_struct<adapted>::value);
static_assert(!is_adapted_struct<not_adapted>::value);
static_assert(!is_adapted_struct<boost::fusion::vector<int,double>>::value);
}