我可以将 BOOST_FUSION_ADAPT_STRUCT 用于继承的东西吗?

Can I use BOOST_FUSION_ADAPT_STRUCT with inherited stuff?

假设我有

struct cat
{
    int tail;
    int head;
};

struct bird
{
    int wing;
    int bursa;
};

如果我这样做...

struct wat : public cat, public bird
{

};

BOOST_FUSION_ADAPT_STRUCT(cat,tail,head)
BOOST_FUSION_ADAPT_STRUCT(bird, wing, bursa)
BOOST_FUSION_ADAPT_STRUCT(wat, wat::cat, wat::bird)

...我无法构建,但如果我像下面这样显式引用继承的对象,它是完全有效的。

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
namespace qi = boost::spirit::qi;
struct wat
{
public:
    cat z;
    bird q;
};

BOOST_FUSION_ADAPT_STRUCT(cat,tail,head)
BOOST_FUSION_ADAPT_STRUCT(bird, wing, bursa)
BOOST_FUSION_ADAPT_STRUCT(wat, z, q)

有什么方法可以让第一个版本正常工作,这样我就可以调整继承的 public 成员的结构吗?我绝对不想做 BOOST_FUSION_ADAPT_STRUCT(wat,tail,head,wing,bursa) 但这似乎是我能找到的继承成员到达那里的唯一方法。

这里有类似的问题:c++/boost fusion handle parent class

简短的问题:不,这不是一项功能。

您可以使用聚合而不是继承,

struct wat {
    cat _cat;
    bird _bird;
};

BOOST_FUSION_ADAPT_STRUCT(wat, _cat, _bird)

但你不会神奇地得到一个扁平的序列。

您可能想在处理融合序列的代码中做的是编写对已知碱基 类 的支持的代码,并在适应的序列成员之外处理它们。

带聚合的演示

Live On Coliru

#include <boost/fusion/include/adapted.hpp>

struct cat {
    int tail;
    int head;
};

struct bird {
    int wing;
    int cloaca;
};

struct wat {
    cat _cat;
    bird _bird;
};

BOOST_FUSION_ADAPT_STRUCT(cat, tail, head)
BOOST_FUSION_ADAPT_STRUCT(bird, wing, cloaca)
BOOST_FUSION_ADAPT_STRUCT(wat, _cat, _bird)

#include <iostream>
#include <boost/fusion/include/at_c.hpp>

template <typename T, int N = 0> void print(T const& obj) {
    namespace fus = boost::fusion;
    if constexpr (fus::traits::is_sequence<T>::value) {
        if (N==0)
            std::cout << "{";

        if constexpr (N < fus::size(obj).value) {
            auto name = boost::fusion::extension::struct_member_name<T, N>::call();
            std::cout << ' ' << name << '=';
            print(fus::at_c<N>(obj));
            std::cout << ';';
            print<T, N+1>(obj);
        } else {
            std::cout << " }";
        }
    } else {
        std::cout << obj;
    }
}

int main() {
    print(wat { {1,2}, {3,4} });
}

版画

{ _cat={ tail=1; head=2; }; _bird={ wing=3; cloaca=4; }; }

带有硬编码碱基列表的演示

Live On Coliru

#include <boost/fusion/include/adapted.hpp>

struct cat {
    int tail;
    int head;
};

struct bird {
    int wing;
    int cloaca;
};

struct wat : cat, bird {
    int something;
    int extra;

    wat(int tail, int head, int wing, int cloaca, int something, int extra)
        : cat{tail, head}, bird{wing, cloaca}, something(something), extra(extra)
    { }
};

BOOST_FUSION_ADAPT_STRUCT(cat, tail, head)
BOOST_FUSION_ADAPT_STRUCT(bird, wing, cloaca)
BOOST_FUSION_ADAPT_STRUCT(wat, something, extra)

#include <iostream>
#include <boost/fusion/include/at_c.hpp>

template <typename... KnownBases>
struct Demo {
    template <typename T, int N = 0> static void print(T const& obj, bool outer_sequence_braces = true) {
        namespace fus = boost::fusion;
        if constexpr (fus::traits::is_sequence<T>::value) {
            if (N==0)
            {
                if (outer_sequence_braces) std::cout << "{";
                print_bases<KnownBases...>(obj);
            }

            if constexpr (N < fus::size(obj).value) {
                auto name = boost::fusion::extension::struct_member_name<T, N>::call();
                std::cout << ' ' << name << '=';
                print(fus::at_c<N>(obj), true/*inner sequences get braces*/);
                std::cout << ';';
                print<T, N+1>(obj, outer_sequence_braces);
            } else {
                if (outer_sequence_braces) std::cout << " }";
            }
        } else {
            std::cout << obj;
        }
    }

    template <typename Base, typename T> static bool print_base(T const& obj) {
        if constexpr (not std::is_same<Base, T>() && std::is_base_of<Base, T>())
            print(static_cast<Base const&>(obj), false);
        return true;
    }

    template <typename... Bases, typename T> static void print_bases(T const& obj) {
        bool discard[] = { print_base<Bases>(obj)... };
        (void) discard;
    }
};

int main() {
    Demo<cat, bird>::print(wat { 1, 2, 3, 4, 5, 6 });
}

版画

{ tail=1; head=2; wing=3; cloaca=4; something=5; extra=6; }