3

I have a template

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

which i would like to partially specialize for two special cases of b:

template<int a>
void MyTemplateClass<a, 2> :: computeSomething()
{
    // Special case for b=2 here
}


template<int a>
void MyTemplateClass<a, 3> :: computeSomething()
{
    // Special case for b=3 here
}

However, as far as i know, partial specialization is invalid for member functions. How can i achieve what i want? Is there any other solution? Thank you!

max66
  • 65,235
  • 10
  • 71
  • 111
mneuner
  • 433
  • 5
  • 25
  • 2
    You need to partially specialize entire class or make several overloaded template methods dealing with particular main class specializations. – user7860670 Jun 06 '17 at 05:59

3 Answers3

5

A possible way is estract compute(), create a base class only for it and specialize this base class.

I mean... if you create a generic version and two specializations for 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; }
 };

you can "specialize" compute in foo through inheritance simply as follows

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

Another possible solution, if you can use at least C++11, is activate/deactivate different version of compute() using SFINAE (std::enable_if) as in the following bar class

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; }
 };

Follows a full compilable example for both ways

#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
 }
max66
  • 65,235
  • 10
  • 71
  • 111
1

Since the typename of template is known, you can add some implement in computeSomething function, to branch the flow, like this:

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

One approach is to extract the computation from this class into a separate one.

Then you can specialise only that computation class:

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 () {} };

Though IMO it's better to not use specialisation, but instead different (descriptive!) names and a compile time conditional to choose between them:

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

If you can already try C++17, then above can be rewritten to:

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
        }
    }
};

Instead of template classes you could also use template functions.

In contrast to using a normal if this avoids evaluation of the "unneeded" code paths, thus making it possible to place otherwise​ ill-formed code there (like a recursive instantiation of the same template).

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63