0

I'm having trouble with recursive template pack unpacking. What I currently have is:

template<T> 
constexpr void register_types() {
    do_something<T>();
    return;
}

template <class T, T2, class... Args> 
constexpr void register_types() {
    do_something<T>();
    register_types<T2, Args...>();
}


int main(int argc, char** argv) {
    register_types<unsigned char, unsigned short, unsigned int, unsigned long long int>();

However I'd like to have a more empty base-case, something like

template<> 
constexpr void register_types() {
    return;
}

template <class T, class... Args> 
constexpr void register_types() {
    do_something<T>();
    register_types<Args...>();
}

However this gives me the error:

src/benchmarks/benchmark_main.cc:68:31: error: ‘register_types’ is not a template function

Is it possible to have an empty parameter pack as the base case? I've seen this post but would like to avoid using SFINAE if possible.

Throckmorton
  • 564
  • 4
  • 17

3 Answers3

4

Is it possible to have an empty parameter pack as the base case?

You can't declare a template function without template parameters.

But you can declare a template function that accept an empty list of template parameters (using default values/types or variadics).

For your case suggestion: try with

template <int = 0> 
constexpr void register_types() {
    return;
}

template <class T, class... Args> 
constexpr void register_types() {
    do_something<T>();
    register_types<Args...>();
}

This way the ground case (the first one) is a regular template function that accept zero template parameter (thanks to the default value).

Accepting an integer and not a type, doesn't give you collision problems when Args... isn't empty.

max66
  • 65,235
  • 10
  • 71
  • 111
2

Yes, you can write an empty base case to terminate recursion (though you still need template parameters).

However, you can avoid having to implement any of that yourself if you just use a fold-expression:

template <class ...Ts> 
constexpr void register_types() {
    (do_something<Ts>(), ...);
}

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

No you cannot use empty Templates Parameters. I do Not No if this can Help in your case. You could use a class to Store the typename for multiple functions.

just a guy
  • 137
  • 7