순수 추상 기본 클래스가 있다고 가정 해 보겠습니다. 클래스 템플릿은이 인터페이스를 구현하며 특수화되어 있습니다. 자, 내 문제는이 전문화는 전문 분야의 하위 클래스를 처리 할 수 있어야한다는 것입니다. 그래서 enable_if를 시도했지만 하위 클래스가 추상적으로 끝납니다 ... 어떻게하면이 문제를 해결할 수 있습니까?C++ 추상 기본 클래스가있는 서브 클래스 템플릿 특수화
// This example doesn't work because a subclass of A does not satisfy the
// specialization for B<T>::foo()
class A {
public:
virtual void foo() = 0;
};
template <class T>
class B : public A {
...
public:
...
void foo();
...
};
void B::foo() {
...
}
template <>
void B<A>::foo() {
...
}
class C : A {
...
public:
...
void foo();
...
};
int main() {
B<C> bar; // I was like "this is gonna work!! :) :D"
bar.foo(); // But this calls B::foo() instead of B<A>::foo()... :'(*sob*
}
그리고 또 다른 예 :이 문제를 해결하는 방법에 대한
// This example doesn't work because B ends up being abstract
class A {
public:
virtual void foo() = 0;
};
template <class T>
class B : public A {
...
public:
...
template <class U=T>
typename std::enable_if<!std::is_base_of<U, A>::value, void>::type
foo() {
...
}
template <class U=T>
typename std::enable_if<std::is_base_of<U, A>::value, void>::type
foo() {
...
}
};
class C : A {
...
public:
...
void foo();
...
};
int main() {
// I got excited thinking this would work \(^.^)/
B<C> bar; // and then it didn't because B is abstract /(-_-)\ ...
bar.foo();
}
어떤 아이디어 예에 의해
? 감사합니다.
보십시오'가상 void foo() = 0' – Ylisar
@Ylisar 죄송합니다, 그것은 예제의 일부에 오타였습니다. 물론 가상이 아니라면 다른 문제가있을 것입니다. – kotakotakota