如何检查助推融合序列是否为自适应结构
How to check if a boost fusion sequence is an adapted struct?
如果一个序列实际上是一个改编的结构,那么在编译时是否有一个特征或元函数或任何东西需要检查,以便我可以获得它的成员名称?我看到人们通过排除来做到这一点,类似于"如果它不是一个向量,但它仍然是一个序列,那么它一定是一个结构"(我之所以编造,是因为我记不清了(。我不认为这是一个充分的条件,可能应该有更好的融合来实现这一点。但是找不到。如果你知道,请分享。谢谢
我不知道是否有更好的方法,但您可以使用:
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);
}
相关文章:
- 添加静态constexpr成员是否会更改结构/类的内存映射
- 具有引用成员的结构是否具有唯一的对象表示形式
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 公共/私有/受保护是否会更改内存中结构的排列?
- 是否有像地图这样的C++结构,但我得到的不是值的键,而是值的句柄?
- 是否可以将结构数组别名为结构成员数组?
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 用户定义的结构是否有默认C++比较器?
- gcc 是否在 2 条短裤的过度对齐结构的比较中错过了优化机会?
- 是否可以跨多个源文件构建 constexpr 数据结构?
- 前向声明时"结构"和"类"关键字是否相同?
- 在 Visual Studio 中调试时,是否可以将一些 C/C++ 结构内容保存到磁盘?
- 是否有像ADTF2这样的结构化属性?
- C++ - 检查结构数据类型中的单词是否为回文
- 当结构在C++中也被引用时,它是否可以返回其成员之一?
- 如何检查助推融合序列是否为自适应结构
- 具有指向函数的指针的类内的结构是否需要类外函数的前向声明
- 对于这个问题,是否有更好的数据结构和算法选择
- 助推.测试检查结构是否相等