Reputation: 425
I have a class which has a template specialization version. However, the former can't see the method implemented by the generic version. How can I make all methods in the generic version visible by the specialized version?
For example:
test.hpp
#include <iostream>
template <typename T>
class A_base{
public:
virtual void foo() = 0;
};
template <typename T>
class A : public A_base<T> {
public:
void foo() override {
std::cout << "foo: generic type" << "\n";
}
};
template <>
class A<int> : public A_base<int>{
public:
void bar() {
std::cout << "bar: int type" << "\n";
}
};
test.cpp
#include "test.hpp"
int main(){
A<int> a;
a.foo(); // expected "foo: generic type"
a.bar(); // expected "bar: int type"
}
Why A<int> a
can't see foo()
?
Upvotes: 2
Views: 45
Reputation: 24738
Why
A<int>
a can't seefoo()
?
By specializing the class template A<T>
for T = int
, you are defining how the class template A<T>
is when T
corresponds to int
, and that specialization (i.e.: A<int>
) you are providing has no member called foo
(the primary template however does).
It is possible to individually specialize the member functions of a class template. So, you can simply specialize the member function bar
of the class template T
for T = int
instead of doing it for the whole class template:
template <>
void A<int>::bar(){
std::cout << "bar: int type" << "\n";
}
Upvotes: 2