可变模板成员函数的部分专门化

Partial specialization of variadic template member function

本文关键字:专门化 函数 成员      更新时间:2023-10-16

当使用可变模板对成员函数进行模板化时,我很难对它们进行专门化。

下面的例子专门针对整个类,它运行良好:

template<typename... Args>
class C;
template<class T, typename... Args>
class C<T, Args...> { };
template<>
class C<> { };
int main() {
    C<int, double> c{};
}

下面的一个没有,尽管它背后的想法与上面的完全相同:

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

我收到以下错误,我不明白是什么原因造成的:

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();
          ^

在专门化函数模板时,是否存在一些我不知道的限制?

G++版本为:G++(Debian 5.2.1-23)5.2.1 20151028

编辑

顺便说一下,我从真实代码中得到的实际问题是:

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

无论如何,简化的例子与真实的例子相似。我希望这些错误并非完全无关。

不能部分专门化函数模板;只允许显式专门化。

使用重载可以获得几乎相同的效果,特别是如果使用诸如标记调度之类的概念。