samedi 23 juin 2018

Empy Pack Variadic Template

Let's say I have some action to perform, at compile time:

enum class Action {A, B};

Now I write a template variadic function which performs a possible combination of action in sequence:

template <Action a>
void applyAction();

template <typename = void>
void applyActions() {}

template <Action a, Action... as>
void applyActions() {
  applyAction<a>();
  applyActions<as...>();
}

This code is fine. Indeed:

void foo() {
  applyActions<Action::A, Action::B>();
}

correctly generates:

call void applyAction<(Action)0>()
call void applyAction<(Action)1>()

GodBolt Example Here


In order to achieve the expansion pack termination, I had to declare the dummy function:

template <typename = void> void applyActions() {}

which is quite "ugly" to me because it gives the possibility to invoke a generic type.

In C++11, Is there a way to declare a variadic function which accepts an empty parameter pack?

Something like:

template <Action.. = {}>
void applyActions() {}


Here a not compilable example because of a call ambiguity. But it gives an idea of what I want to achieve.

Aucun commentaire:

Enregistrer un commentaire