3

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

这是一个例子

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

3 回答 3

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
}
于 2012-10-18T11:11:20.080 回答
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; }
于 2012-10-17T15:35:37.493 回答
0

为了回应与亚历克斯的讨论(见约翰迪布林的回答的评论),这就是我的意思(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>();
}
于 2012-10-18T15:51:04.827 回答