我正在实现一个有限状态机,其中所有可能的状态都存储在std::tuple.
这是我面临的问题的最小编译示例及其 godbolt 链接https://godbolt.org/z/7ToKc3T3W:
#include <tuple>
#include <stdio.h>
struct state1 {};
struct state2 {};
struct state3 {};
struct state4 {};
std::tuple<state1, state2, state3, state4> states;
template<size_t Index>
void transit_to()
{
auto state = std::get<Index>(states);
//Do some other actions over state....
}
void transit_to(size_t index)
{
if (index == 0) return transit_to<0>();
if (index == 1) return transit_to<1>();
if (index == 2) return transit_to<2>();
if (index == 3) return transit_to<3>();
}
int main()
{
for(int i=0; i<=3; …Run Code Online (Sandbox Code Playgroud)