可变参数模板成员函数的部分特化
Partial specialization of variadic template member function
当成员函数使用可变参数模板进行模板化时,我正在为成员函数的特化而苦恼。
以下示例对整个 class 进行了专门化,并且工作正常:
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
无论如何,缩小的例子和真实的相似。我希望这些错误不是完全无关的。
您不能部分特化函数模板;只允许显式专业化。
使用重载可以获得几乎相同的效果,尤其是当您使用 tag dispatching.
等概念时
当成员函数使用可变参数模板进行模板化时,我正在为成员函数的特化而苦恼。
以下示例对整个 class 进行了专门化,并且工作正常:
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
无论如何,缩小的例子和真实的相似。我希望这些错误不是完全无关的。
您不能部分特化函数模板;只允许显式专业化。
使用重载可以获得几乎相同的效果,尤其是当您使用 tag dispatching.
等概念时