继承成员函数的模板特化

时间:2012-10-17 15:31:21

标签: c++ templates

我们想专门化基类的成员函数。但是,它没有编译。有没有人知道任何编译的替代方案?

这是一个例子

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

3 个答案:

答案 0 :(得分:2)

最终,我们使用重载解决了它。

以下是基类的样子

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

以下是使用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
}

答案 1 :(得分:0)

这将编译,但调用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
}

如果您希望拨打Foo<char>() - 或任何不是您特别专业的类型 - 那么这是有效的。如果您想要一个适用于所有类型的非专业实现,那么您还需要添加Foo()的非专业实现:

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

答案 2 :(得分:0)

在回应与Alex的讨论时(见John Dibling的回答),这就是我的意思(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>();
}