Partially specialized implementations of member functions

I am currently reorganizing some code that explicitly specializes in 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 have 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();
};

These two additional parameters just add typedefs to my class, so the functionality at runtime doesn't change. Is there a way to keep the (now partially) specialized implementation of the bar? I seem to be unable to understand the syntax, and I have a suspicion that this may not be possible.

Edit: I'm looking for something like:

template <class EXTRA0, class EXTRA1>
void foo<SomeType, EXTRA0, Sometype, EXTRA1>::bar()
{
   /* specialized implementation */
}

which does not seem to compile.

+3
source share
4 answers

You are right, this is impossible.

, Foo , . - .

.

+3

, , . - :

template <class S, class EXTRA0, class T, class EXTRA1>
class FooBase
{
    void bar();
};

template <class S, class EXTRA0, class T, class EXTRA1>
void FooBase<S, EXTRA0, T, EXTRA1>::bar()
{ /* Generic stuff */ }

template <class S, class EXTRA0, class T, class EXTRA1>
class Foo
    : public FooBase <S, EXTRA0, T, EXTRA1>
{ };

template <class EXTRA0, class EXTRA1>
class Foo<int, EXTRA0, int, EXTRA1>
    : public FooBase <int, EXTRA0, int, EXTRA1>
{
    void bar ();
};

template <class EXTRA0, class EXTRA1>
void Foo<int, EXTRA0, int, EXTRA1>::bar()
{ /* Some special function */ }
+1

, , bar(), ( , SomeType):

template <class S, class T>
class FooBase
{
       // All other members 
};

template <class S, class EXTRA0, class T, class EXTRA1>
class Foo:public FooBase<S,T>
{
public:
      void bar()
      {

      }
};

struct SomeType {};

template <class EXTRA0, class EXTRA1>
class Foo<SomeType,EXTRA0,SomeType,EXTRA1>:public FooBase<SomeType,SomeType>
{
public:
    void bar()
    {

    }
};

int main()
{
    Foo<SomeType,int,SomeType,int> b;
    b.bar();
}
+1

, specialize, :

#include <iostream>

typedef int SomeType;

template <class A, class B>
class BarFunctor {
public:
    void operator()() {
        std::cout << "generic" << std::endl;
    }
};

template <>
class BarFunctor<SomeType, SomeType> {
public:
    void operator()() {
        std::cout << "special" << std::endl;
    }
};

template <class S, class T, class EXTRA0, class EXTRA1>
class Foo {
public:
    void helloWorld() {
        std::cout << "hello world !" << std::endl;
    }

    void bar() {
        return _bar();
    }

private:
    BarFunctor<S, T> _bar;
};

int main() {

    Foo<char, char, char, char> gen;
    Foo<SomeType, SomeType, char, char> spe;
    gen.helloWorld();
    spe.helloWorld();
    gen.bar();
    spe.bar();
    return 0;
}
+1
source

All Articles