I am experimenting with finate state machines and std::variant<..>
std::variant will hold all possible states The states will be defined in State classes.
I'd like to have std::monostate as the first type of State variant to use it as a "do not change state" value. So the definition would be : first type is std::monostate, second type is initial state of the fsm.
As the std::variant is initialized to it's first element, I wanted to use emplace<1> in the constructor.
template <typename StateVariant>
class fsm
{
public:
StateVariant state;
fsm()
{
state.emplace<1>();
};
};
struct Initial{};
struct Running{};
using State = std::variant<std::monostate,Initial,Running>;
fsm<State> myFSM;
But that gives compiletime error :
..\fsm_emplace.cpp: In constructor 'fsm<StateVariant>::fsm()':
..\fsm_emplace.cpp:15:20: error: expected primary-expression before ')' token
15 | state.emplace<1>();};
However, when i use the same construct in non template code :
State myState;
void setup()
{
myState.emplace<1>();
}
Is there a restriction of usage for the emplace function ?