当使用可变参数模板对成员函数进行模板化时,我正在努力应对成员函数的专业化问题。
以下示例专门针对整个类,并且效果很好:
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
无论如何,简化后的示例与真实示例相似。我希望这些错误不是完全无关的。
您不能部分专门化功能模板;仅允许显式专业化。
使用重载可以获得几乎相同的效果,尤其是在使用诸如标签分派之类的概念时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句