Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial specialization of variadic template member function

I'm struggling with specializations of member functions when they are templated using variadic template.

The following example specializes a whole class and it works fine:

template<typename... Args>
class C;

template<class T, typename... Args>
class C<T, Args...> { };

template<>
class C<> { };

int main() {
    C<int, double> c{};
}

The following one does not, even though the idea behind it is exactly the same of the one above:

class F {
    template<typename... Args>
    void f();
};

template<class T, typename... Args>
void F::f<T, Args...>() { }

int main() {
}

I'm getting the following error and I don't understand what it's due to:

main.cpp:7:23: error: non-type partial specialization ‘f<T, Args ...>’ is not allowed
 void F::f<T, Args...>() { }
                       ^
main.cpp:7:6: error: prototype for ‘void F::f()’ does not match any in class ‘F’
 void F::f<T, Args...>() { }
      ^
main.cpp:3:10: error: candidate is: template<class ... Args> void F::f()
     void f();
          ^

Is there some constraints I'm not aware of when specializing function template?

G++ version is: g++ (Debian 5.2.1-23) 5.2.1 20151028

EDIT

By the way, the actual problem I'm getting from the real code is:

non-class, non-variable partial specialization ‘executeCommand<T, Args ...>’ is not allowed

Anyway, the reduced example is similar to the real one. I hope the errors are not completely unrelated.

like image 453
skypjack Avatar asked Nov 17 '15 09:11

skypjack


1 Answers

You cannot partially specialize function templates; only explicit specialization is allowed.

You can get pretty much the same effect using overloading, especially if you use concepts such as tag dispatching.

like image 51
TartanLlama Avatar answered Sep 20 '22 13:09

TartanLlama