部分专门化成员函数实现 [英] Partially specializing member-function implementations
问题描述
我目前正在重构一些代码,该代码明确专用于具有两个模板参数的类模板的成员函数.
I'm currently refactoring some code the explicitly specializes a member function of a class template with two template parameters.
template <class S, class T>
class Foo
{
void bar();
};
template <class S, class T>
void Foo<S, T>::bar()
{ /* Generic stuff */ }
template <>
void Foo<SomeType, SomeType>::bar()
{ /* Some special function */ }
现在,我添加了更多模板参数,因此该类现在看起来像这样:
Now I added some more template parameters, so the class now looks like this:
template <class S, class EXTRA0, class T, class EXTRA1>
class Foo
{
void bar();
};
这两个额外的参数只是将typedef添加到我的类中,因此运行时功能并没有真正改变.有什么方法可以保留bar的(现在是部分)专业化的实现?我似乎无法弄清楚它的语法,并且我预感这可能是不可能的.
These two extra parameters just add typedefs to my class, so the run-time functionality doesn't really change. Is there any way I can keep the (now partially) specialized implementation of bar? I can't seem to figure out the syntax for that and I have a hunch that it might not be possible.
我正在寻找类似的东西
I'm looking for something like:
template <class EXTRA0, class EXTRA1>
void foo<SomeType, EXTRA0, Sometype, EXTRA1>::bar()
{
/* specialized implementation */
}
似乎没有编译..
推荐答案
您是正确的,这是不可能的.
You are correct, it is not possible.
您可以做的是在新的 Foo
内创建一个辅助成员类模板,并将专用函数作为非模板成员函数放在其中.专门使用助手类而不是函数.
What you can do is create a helper member class template inside the new Foo
, and place the specialized function inside it as a non-template member function. Specialize the helper class instead of the function.
另一种选择是将专业化转化为非模板重载.
Another alternative is to turn the specialization into a non-template overload.
这篇关于部分专门化成员函数实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!