boost-ext / sml

C++14 State Machine library
https://boost-ext.github.io/sml
Boost Software License 1.0
1.15k stars 178 forks source link

Action cannot capture dependency by const& #195

Closed segfault-magnet closed 5 years ago

segfault-magnet commented 6 years ago

Compile fails when trying to catch a dependency by const& when it was constructed as non const (at the time of constructing the state machine)

#include <boost/sml.hpp>

using namespace boost::sml;

struct DependencyWrapper {};
struct Transitions {
  auto operator()() {
    return make_transition_table(
        *"start"_s +
            "event"_e / [](const DependencyWrapper& deps,  // < -- by const&
                           const auto& event) {} = X);
  }
};

int main(int argc, char const* argv[]) {
  DependencyWrapper dw{};
  const DependencyWrapper const_dw{};

  sm<Transitions> machine(dw);        // fails
  sm<Transitions> machine(const_dw);  // succeeds

  return 0;
}

Is there a way to correct this in sml so I can capture the dependency by const& regardless of how it was constructed.

krzysztof-jusiak commented 5 years ago

Seems to work now -> https://wandbox.org/permlink/5iQnf9qyWwxGl8PS Notice that in the current design const/non-const types are distinguishable by the SML, hence they have to be passed separately or injected using di framework. That will most likely change in the future though.