【发布时间】:2018-11-03 03:20:58
【问题描述】:
当使用可变参数模板对其进行模板化时,我在成员函数的特化上苦苦挣扎。
以下示例专门针对整个类,并且运行良好:
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
无论如何,简化的示例与真实示例相似。我希望这些错误不是完全无关的。
【问题讨论】:
-
函数模板不能部分特化。
-
你的意思是成员函数模板吗?
-
@skypjack:会员而非会员。
-
该死的。完全忘记了,对不起。这个错误确实很清楚,但是当你理解它时它就足够清楚了。谢谢你。 :-)
-
一个很好的解释为什么不专门化函数模板:gotw.ca/publications/mill17.htm
标签: c++ templates c++11 variadic-templates