Alex Shtoff
Alex Shtoff

Reputation: 2640

Template specialization of inherited member function

We would like to specialize member functions of a base class. However, it does not compile. Does anybody know of any alternative that does compile?

Here is an example

struct Base
{
    template<typename T>
    void Foo()
    {
        throw "Foo() is not defined for this type";
    }
};

struct Derived : public Base
{
    template<>
    void Foo<int>() { cout << "Foo<int>()" << endl; } // compile error (cannot specialize members from a base class)

    template<>
    void Foo<double>() { cout << "Foo<double>()" << endl; }  // compile error (cannot specialize members from a base class)
};

Upvotes: 3

Views: 2558

Answers (3)

dyp
dyp

Reputation: 39101

In response to the discussion with Alex (see comments of the answer of John Dibling), this is what I meant (SSCCE):

#include <iostream>
using namespace std;

struct Base
{
    template<typename T>
    void Foo()
    {
        //static_assert(false, "Foo() is not defined for this type");
        throw "Foo() is not defined for this type";
    }
};
    // you can add as many specializations in Base as you like
    template <>
    void Base::Foo<char>()  {  cout << "Base::Foo<char>()" << endl;  }


struct Derived : public Base
{
    // just provide a default implementation of Derived::Foo
    // that redirects the call to the hidden Base::Foo
    template < typename T >
    void Foo()
    {  Base::Foo<T>();  }
};
    // the specializations for Derived
    template<>
    void Derived::Foo<int>() { cout << "Foo<int>()" << endl; }

    template<>
    void Derived::Foo<double>() { cout << "Foo<double>()" << endl; }


struct Derived_wo_specialization : public Base
{
    /* nothing */
};


int main()
{
    Derived d;
    d.Foo<char>();
    d.Foo<double>();

    Derived_wo_specialization dws;
    dws.Foo<char>();
    dws.Foo<double>();
}

Upvotes: 0

Alex Shtoff
Alex Shtoff

Reputation: 2640

Eventually, we solved it using overloading.

Here is how the base class looks like

struct Base
{
    template<typename T>
    class OfType {}

    template<typename T>
    void Foo(OfType<T>) { static_assert(false, "Foo is not implemented for this type. Please look in the compiler error for more details."); }
};

struct Derived : public Base
{
    using Base::Foo;

    void Foo(OfType<int>) { // here comes logic for ints }
    void Foo(OfType<double>) { // here comes logic for doubles }
};

Here is an example of client code that uses Foo()

template<typename S>
class ClassThatUsesFoo
{
    private: S s;

    template<typename T>
    void Bar(T item) 
    {  
        s.Foo(Base::OfType<T>());  // this is the code that uses Foo
        DoSomeStuffWithItem(item); 
    } 
};

void main()
{
    ClassThatUsesFoo<Derived> baz;
    baz.Bar(12); // this will internally use Foo for ints
    baz.Bar(12.0); // this will use Foo for doubles
    baz.Bar("hello world"); // this will give a verbose compile error
}

Upvotes: 4

John Dibling
John Dibling

Reputation: 101446

This will compile, except for the call to Foo<char>():

#include <iostream>
#include <string>
using namespace std;

struct Base
{
    template<typename T>
    void Foo()
    {
        throw "Foo() is not defined for this type";
    }
};

struct Derived : public Base
{
    template<typename T> void Foo();
};

template<>
void Derived::Foo<int>() { cout << "Foo<int>()" << endl; }

template<>
void Derived::Foo<double>() { cout << "Foo<double>()" << endl; }

int main()
{
    Derived derived;

    // this is client code
    derived.Foo<int>(); 
    derived.Foo<double>(); 
    derived.Foo<char>();   // this throws
}

If you want the call to Foo<char>() -- or any type not specifically specialized by you -- then this works. If you want a non-specialized implementation that works for all types, then you need to add a non-specialized implementation of Foo() as well:

template<typename T> 
void Derived::Foo() { cout << "generic" << endl; }

Upvotes: 0

Related Questions