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

时间:2018-07-11 15:12:30

标签: c++ boost

无法确定 TSm _ 中违反了哪个规则。 Sm _ 已编译,而 TSm _ 不是:

  

错误C2974:“ boost :: mpl :: vector”:“ T0”的无效模板参数,   预期的类型

     

错误C2974:“ boost :: mpl :: vector”:“ T3”的无效模板参数,   预期的类型

区别在于 TSm _ 是模板,而 Sm _ 不是。

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

请帮助

更新

我发现了如何对其进行编译:由于a_row不是类型而是模板,因此其别名也应该是模板

requests

是对的,这是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条规则吗?

1 个答案:

答案 0 :(得分:1)

使用模板状态机类定义过渡表时,可以使用rowa_rowg_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