Home > Enterprise >  Recursive parameter pack functions without parameters C
Recursive parameter pack functions without parameters C

Time:11-16

I'm trying to create a recursive parameter pack function as follows:

template <class T, class ... Ts>
void myFunction()
{
    execute<T>();

    myFunction<Ts...>();
}

This doesn't compile with the error:

error C2672: 'Test::myFunction': no matching overloaded function found.

Does anyone know how to do what I'm trying to achieve? Thanks

CodePudding user response:

If you can use C 17 or newer, you can skip the recursion and use a fold expression like

template<class... Ts>
void myFunction()
{
    (execute<Ts>(), ...);
}

CodePudding user response:

Alright I was close, this works:

template<class T>
void execute()
{
    // Do whatever
}

template <class T>
void myFunction()
{
    execute<T>();
}
template<class First, class Second, class... Rest>
void myFunction()
{
    myFunction<First>();

    myFunction<Second, Rest...>();
}
  •  Tags:  
  • c
  • Related