'Calling Substate machine constructor using boost-ext::sml

I was wondering if its possible to initialize a substate machine by using a non-default constructor. I know one can do:

sub = sml::state<Sub>;

And use the sub I.e:

auto sub = sml::state<Sub>;
return make_transition_table(
     *"init"_s + event<Start>   / print(data_) = sub
     ,state<Sub> + event<End>  / print(data_) = X
 );

But if the Sub statemachine has a non-default constructor, is it possible to use that constructor to initiate the machine and then use that in the Composite machine?

Example:

struct Sub
{
    Sub(int){}
    auto operator()() const {
        using namespace sml;
        return make_transition_table(
            *"Entry"_s + event<ES1> = X
        );
    }

};

Here is an example: https://cpp.godbolt.org/z/zjvWsvscY

Complete example code:

namespace sml = boost::sml;

struct Context
{
    int x_{};
};

struct Data
{
    int y_{};
};


struct Start{};
struct End{};
struct ES1{};

auto print(Data data)
{
    return [data](Context& ctx) 
    {
        std::cout << "Context: " << ctx.x_ << " data: "<< data.y_ << '\n';
        ++ctx.x_;
    };
}

struct Sub
{
    //Sub(int){}
    auto operator()() const {
        using namespace sml;
        return make_transition_table(
            *"Entry"_s + event<ES1> = X
        );
    }

};



struct Composite
{
    Composite(Data data): data_{std::move(data)}{}
    auto operator()() const {
        using namespace sml;
        auto sub = sml::state<Sub>;
        return make_transition_table(
            *"init"_s + event<Start> / print(data_) = sub
            ,state<Sub> + event<End>  / print(data_) = X
        );
    }

    Data data_;
};


int main()
{
    //Sub sub{};

    Data data;
    Context ctx;
    sml::sm<Composite> sm(Composite{data}, ctx);
    sm.process_event(Start{});
    sm.process_event(ES1{});
    sm.process_event(End{});
    return 0;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source