c++ - 无法使用模板参数编译 boost 元状态机

标签 c++ boost

无法确定 TSm_ 违反了哪条规则。 Sm_ 已编译,TSm_ 未编译:

error C2974: 'boost::mpl::vector': invalid template argument for 'T0', type expected

error C2974: 'boost::mpl::vector': invalid template argument for 'T3', type expected

区别在于 TSm_ 是模板而 Sm_ 不是。

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>

namespace msm = boost::msm;
namespace mpl = boost::mpl;

struct Event {};

class Sm_ : public msm::front::state_machine_def<Sm_>
{
public:
    using Me = Sm_;
    struct First : public msm::front::state<>
    {};
    using initial_state = First;
    struct Second : public msm::front::state<>
    {};
    void trans(const Event& /*e*/)
    {}
    struct transition_table : public mpl::vector<
        a_row<First, Event, Second, &Me::trans>
    >
    {};
};

using Sm = msm::back::state_machine<Sm_>;

// broken one

enum class Side : char
{
    Buy = 0, Sell = 1
};

template <Side side>
class TSm_ : public msm::front::state_machine_def<TSm_<side>>
{
public:
    using Me = TSm_<side>;
    using Base = msm::front::state_machine_def<Me>;
    using Base::a_row;
    struct First : public msm::front::state<>
    {};
    using initial_state = First;
    struct Second : public msm::front::state<>
    {};
    void trans(const Event& /*e*/)
    {}
    struct transition_table : public mpl::vector<
        a_row<First, Event, Second, &Me::trans> // compilation is failed here
    >
    {};
};

template <Side side>
using TSm = msm::back::state_machine<TSm_<side>>;

请帮忙

更新

我找到了编译的方法:因为 a_row 不是类型而是模板,它的别名也应该是模板

struct Begin {};

template <Side side>
class TSm_ : public msm::front::state_machine_def<TSm_<side>>
{
public:
    //-----------------------------------------------------------------------------------------------------
    using Me = TSm_<side>;
    using Base = msm::front::state_machine_def<Me>;
    template<
        typename T1
        , class Event
        , typename T2
        , void (Me::*action)(Event const&)
    >
    using a_row = typename Base::a_row;
    //-----------------------------------------------------------------------------------------------------
    struct First : public msm::front::state<>
    {};
    //-----------------------------------------------------------------------------------------------------
    using initial_state = First;
    //-----------------------------------------------------------------------------------------------------
    struct Second : public msm::front::state<>
    {};
    //-----------------------------------------------------------------------------------------------------
    void trans(const Begin& /*e*/)
    {}
    //-----------------------------------------------------------------------------------------------------
    struct transition_table : public mpl::vector<
        a_row<First, Begin, Second, &Me::trans>
    >
    {};
};

这条规则对不对2) An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id

最佳答案

当您使用模板状态机类定义转换表时,您可以使用 row 的仿函数前端 insted | , a_row , 和 g_row家庭。

参见 https://www.boost.org/doc/libs/1_67_0/libs/msm/doc/HTML/ch03s03.html

如果你使用仿函数前端,你不需要关心msm::front::state_machine_def的子类。是不是模板。

这是根据您的代码更新后的代码:

#include <iostream>

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>

// include functor_row
#include <boost/msm/front/functor_row.hpp>

namespace msm = boost::msm;
namespace msmf = msm::front;
namespace mpl = boost::mpl;

struct Event {};

// broken one

enum class Side : char
{
    Buy = 0, Sell = 1
};

template <Side side>
class TSm_ : public msm::front::state_machine_def<TSm_<side>>
{
public:
    using Me = TSm_<side>;
    using Base = msm::front::state_machine_def<Me>;
    struct First : public msm::front::state<>
    {};
    using initial_state = First;
    struct Second : public msm::front::state<>
    {};

    // Replace the member function trans() with the functor trans.
    struct trans {
            template <class Fsm, class SourceState, class TargetState>
            void operator()(Event const&, Fsm&, SourceState&, TargetState&) const {
            std::cout << "called" << std::endl;
        }
    };

    struct transition_table : public mpl::vector<
                 //from     event    to      action,    guard
        msmf::Row <First,   Event,   Second, Me::trans, msmf::none >
        // This is functor front-end
    >
    {};
};

template <Side side>
using TSm = msm::back::state_machine<TSm_<side>>;

int main() {
    TSm<Side::Buy> sm;
    sm.start();
    sm.process_event(Event());
}

运行演示:https://wandbox.org/permlink/e9Qoc2xW3TTGhsmu

关于c++ - 无法使用模板参数编译 boost 元状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288962/

相关文章:

c++ - Clang 模块与 std <iterator> 和 <boost/move/iterator.hpp> 交互

c++ - boost 如何知道要链接哪个 LIB 以及如何更改它?

c++ - 如何在 C++ 中序列化一个保留指向其他对象的指针的对象?

c++ - 如何让 BOOST_TEST_MESSAGE 显示在屏幕上?

c++ - 在 C++ 的 CC 编译器中使用 argv[]

c++ - 三次贝塞尔曲线上到给定点的最近点

c++ - 柔性 : trying to generate a C++ lexer using Flex; "unrecognized rule" error

c++ - 不可用的公共(public)变量

c++ - __transaction_atomic 未启用事务内存支持

c++ - Boost 1.41.0:boost::locale 替代方案?