继承成员功能的模板专业化

Template specialization of inherited member function

本文关键字:专业化 成员 功能 继承      更新时间:2023-10-16

我们想专业化基类的成员功能。但是,它没有编译。有人知道有任何编译的选择吗?

这是一个示例

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

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

这是基类的样子

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
}

这将编译,除了呼叫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; }

响应与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>();
}