I'm currently playing around with template metaprogramming. I'm trying to make a finite state machine by using tmp. I know that there are several implementations in the web but I want to implement one by myself as an exercise.
I have a class called Condition
which is the base class for the condition of a transition between two states. One implementation is the AnyCondition
class:
template<class Input, Input comp, Input ... comps >
class AnyCondition: public Condition<Input>
{
public:
AnyCondition() {}
bool operator()(const Input& input) const override
{
return input == comp || AnyCondition<Input, comps...>()(input);
}
};
The thing here is, that the compiler will expand this recursivly, which results in a lot of recursive calls at runtime due to the input
parameter. It should be more efficent, if the expanded code would be a statement like:
bool operator()(const Input& input) const override
{
return input == comp1 || input == comp2 || input == comp3...
}
Is this possible somehow?
va_list
is not template metaprogramming. The OP is looking for a compile time solution, with type safety. – Yarrow