如何检查助推融合序列是否为自适应结构

How to check if a boost fusion sequence is an adapted struct?

本文关键字:是否 结构 自适应 融合 何检查 检查      更新时间:2023-10-16

如果一个序列实际上是一个改编的结构,那么在编译时是否有一个特征或元函数或任何东西需要检查,以便我可以获得它的成员名称?我看到人们通过排除来做到这一点,类似于"如果它不是一个向量,但它仍然是一个序列,那么它一定是一个结构"(我之所以编造,是因为我记不清了(。我不认为这是一个充分的条件,可能应该有更好的融合来实现这一点。但是找不到。如果你知道,请分享。谢谢

我不知道是否有更好的方法,但您可以使用:

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及其变体(您需要用assoc_struct_tag替换struct_tag才能工作(。我认为(对于您的用例来说,这可能是一个问题(,对于使用BOOST_FUSION_ADAPT_ADT改编的类,这也将返回true。

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);
}