如何继承具有一堆分区专业化接口的类

时间:2022-03-12 07:50:30

Assume I have a class A and a template function I<T>. I have a partial version I<A>.

假设我有一个A类和一个模板函数I 。我有一个部分版本

Now, I need to add some data members to A. So I try to inherit A with a class B. However, function I<T> instead of I<A> will be executed now.

现在,我需要向A添加一些数据成员。所以我尝试使用类B继承A.但是,现在将执行函数I 而不是I

Any chance I can get the extended class B without losing interface I<A>? I prefer to not modifying the functions I and class A, because they are really complicated actually and belong to third-party.

我有机会获得扩展的B级而不会丢失接口I 吗?我更喜欢不修改我和A类的功能,因为它们实际上非常复杂并且属于第三方。

** Manually casting is not workable too, since B& will be cast to A.

**手动投射也不可行,因为B&将被投射到A.

Edited:

What i am really interested at is why polymorphism seems doesn't work fine with partitional specialization here? Is something wrong or just C++ behaves so?

我真正感兴趣的是为什么多态现象似乎不适用于分区专业化?有什么不对或只是C ++表现如此?

Example Code

#include<iostream>
using namespace std;

class A {};

class B: public A {};

template <typename T>
void print(T t) {
    cout << "base template method" << endl;
}

template <>
void print(A t) {
    cout << "partitional template method" << endl;
}

int main() {
    A a;
    print(a);       // partitional method
    B b;
    print(b)            // base method
    print<A>(b);        // partitional method
}

I<A>(b) works fine, but addtional <A> is also unacceptable for my case too. I need I(b) works fine.

(b)工作正常,但对我的情况来说,附加也是不可接受的。我需要我(b)工作正常。

1 个解决方案

#1


0  

The only way to have B b; I(b); result in the specialisation of I for A is to have a specialisation for B that calls it. You can however assign a B to an A& variable, and pass that to I, and that will use the A specialisation.

拥有B b的唯一方法; I(B);导致I for A的专业化是为了调用它的B的专门化。但是,您可以将B分配给A&变量,并将其传递给I,这将使用A特化。

Template specialisation is only for exact matches of type after deduction.

模板专精化仅适用于扣除后的类型的精确匹配。

#include<iostream>
using namespace std;

class A {};

class B: public A {};

template <typename T>
void print(T& t) {
    cout << "base template method" << endl;
}

template <>
void print(A& t) {
    cout << "specialisation for A of template method" << endl;
}

template <>
void print(B& t) {
    cout << "specialisation for B of template method" << endl;
    print<A>(t);
}

int main() {
    A a;
    print(a);       // A specialisation used
    B b;
    print(b);       // B specialisation used, which calls A specialisation
    print<A>(b);    // A specialisation used
    A & ab = b;
    print(ab);      // A specialisation used
}

#1


0  

The only way to have B b; I(b); result in the specialisation of I for A is to have a specialisation for B that calls it. You can however assign a B to an A& variable, and pass that to I, and that will use the A specialisation.

拥有B b的唯一方法; I(B);导致I for A的专业化是为了调用它的B的专门化。但是,您可以将B分配给A&变量,并将其传递给I,这将使用A特化。

Template specialisation is only for exact matches of type after deduction.

模板专精化仅适用于扣除后的类型的精确匹配。

#include<iostream>
using namespace std;

class A {};

class B: public A {};

template <typename T>
void print(T& t) {
    cout << "base template method" << endl;
}

template <>
void print(A& t) {
    cout << "specialisation for A of template method" << endl;
}

template <>
void print(B& t) {
    cout << "specialisation for B of template method" << endl;
    print<A>(t);
}

int main() {
    A a;
    print(a);       // A specialisation used
    B b;
    print(b);       // B specialisation used, which calls A specialisation
    print<A>(b);    // A specialisation used
    A & ab = b;
    print(ab);      // A specialisation used
}