2017-06-06 93 views
2

我有一个模板C++模板偏特:成员函数

template <int a, int b> 
class MyTemplateClass 
{ 
    // .... 
    void computeSomething(); 
}; 

,我想部分专门用于B的两种特殊情况:

不过,据我所知,部分专业化对于成员功能无效。我怎样才能达到我想要的?还有其他解决方案吗?谢谢!

+2

您需要部分专业类或者做处理特定的主类专业化几个重载模板方法一个完整的编译例子。 – VTT

回答

2

一个可能的方法是estract compute(),只为它创建一个基类并专门化这个基类。

我的意思是......如果你创建fooSub

template <int a, int b> 
struct fooSub 
{ 
    void compute() 
    { std::cout << "- foo generic compute()" << std::endl; } 
}; 

template <int a> 
struct fooSub<a, 2> 
{ 
    void compute() 
    { std::cout << "- foo compute() for 2" << std::endl; } 
}; 

template <int a> 
struct fooSub<a, 3> 
{ 
    void compute() 
    { std::cout << "- foo compute() for 3" << std::endl; } 
}; 

一个通用版本和两个专业可以在“专注”通过继承在foo简单地计算如下

template <int a, int b> 
struct foo : public fooSub<a, b> 
{ }; 

另一种可能的解决方案,如果您至少可以使用C++ 11,则使用SFINAE(std::enable_if)启用/禁用不同版本的compute(),如下面的bar

template <int a, int b> 
struct bar 
{ 
    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b != 2) && (b != 3)>::type 
     compute() 
    { std::cout << "- bar generic compute()" << std::endl; } 

    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b == 2)>::type compute() 
    { std::cout << "- bar compute() for 2" << std::endl; } 

    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b == 3)>::type compute() 
    { std::cout << "- bar compute() for 3" << std::endl; } 
}; 

遵循两种方式

#include <iostream> 
#include <type_traits> 

template <int a, int b> 
struct fooSub 
{ 
    void compute() 
    { std::cout << "- foo generic compute()" << std::endl; } 
}; 

template <int a> 
struct fooSub<a, 2> 
{ 
    void compute() 
    { std::cout << "- foo compute() for 2" << std::endl; } 
}; 

template <int a> 
struct fooSub<a, 3> 
{ 
    void compute() 
    { std::cout << "- foo compute() for 3" << std::endl; } 
}; 

template <int a, int b> 
struct foo : public fooSub<a, b> 
{ }; 

template <int a, int b> 
struct bar 
{ 
    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b != 2) && (b != 3)>::type 
     compute() 
    { std::cout << "- bar generic compute()" << std::endl; } 

    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b == 2)>::type compute() 
    { std::cout << "- bar compute() for 2" << std::endl; } 

    template <int bb = b> 
    typename std::enable_if<(b == bb) && (b == 3)>::type compute() 
    { std::cout << "- bar compute() for 3" << std::endl; } 
}; 


int main() 
{ 
    foo<0, 1>{}.compute(); // print - foo generic compute() 
    foo<1, 2>{}.compute(); // print - foo compute() for 2 
    foo<2, 3>{}.compute(); // print - foo compute() for 3 

    bar<2, 1>{}.compute(); // print - bar generic compute() 
    bar<3, 2>{}.compute(); // print - bar compute() for 2 
    bar<4, 3>{}.compute(); // print - bar compute() for 3 
} 
+0

非常干净的解决方案,谢谢! – macmallow

1

由于模板的类型名称是已知的,你可以添加一些实施computeSomething功能,分支流程,就像这样:

template <int a, int b> 
class MyTemplateClass 
{ 
    // .... 
    void computeSomething() 
    { 
     if (b == 2) { computeWith2(); } 
     else if (b == 3) { computeWith3(); } 
     else { computerNormally(); } 
    }; 
} 
1

一种方法是从这个类的计算提取到一个单独的一个。

然后你就可以专注只计算类:

template <int a, int b> 
class MyTemplateClass 
{ 
    // .... 
    void computeSomething() { 
     Computation<a, b> c; 
     c.compute(); 
    } 
}; 
template <int a, int b> 
struct Computation { void compute() {} }; 
template <int a> 
struct Computation<a, 2> { void compute() {} }; 
template <int a> 
struct Computation<a, 3> { void compute() {} }; 

虽然国际海事组织,最好不使用专业化,而是不同(描述!)的名称和编译时间条件两者之间进行选择:

template<bool Condition, 
     typename Then, 
     typename Else> 
using if_t = typename std:: conditional< 
    Condition, Then, Else>:: type; 

template <int a, int b> 
class MyTemplateClass 
{ 
    // .... 
    using Computation = 
     if_t<b == 2, 
      B2Comp<a>, 
      if_t<b == 3, 
        B3Comp<a>, 
        DefaultComp<a, b> > >; 
    void computeSomething() { 
     Computation c; 
     c.compute(); 
    } 
}; 
// Add (template) classes, B3Comp and DefaultComp 

如果你已经可以尝试C++ 17,则上述可改写为:

template <int a, int b> 
class MyTemplateClass 
{ 
    // .... 
    void computeSomething() { 
     if constexpr (b == 2) { 
      B2Comp<a> comp; 
      comp.compute(); 
     } else if constexpr (b == 3) { 
      B3Comp<a> comp; 
      comp.compute(); 
     } else { 
      DefaultComp<a, b> comp; 
      comp.compute(); 
      // Or just put the code here, if it's short 
     } 
    } 
}; 

您可以使用模板函数来代替模板类。

如果避免评估“不需要”的代码路径,从而可以在那里放置其他不合格的代码(如同一个模板的递归实例化),与使用正常代码相反。