Re: g++ and subclass template specialization
vilarneto@gmail.com wrote:
Hello everyone,
Today I started to use template specializations in a project and
suddenly faced a curious problem. Following is a complete example
that shows the situation:
----------
class A {
};
template<class T>
class B : public A {
};
template<>
class B<int> {
};
So, B<T> is a descendant of A for all T but 'int'. B<int> is *not*
a descendant of A. That's what you wrote.
int main(int argc, char *argv[]) {
A *a;
a = new B<double>;
a = new B<int>;
return 0;
}
----------
g++ 3.4.6, refuses to compile the 2nd assignment, issuing a "cannot
convert `B<int>*' to `A*' in assignment" error. However, under my
concepts B is a subclass of A (as expected, the compiler accepts the
1st assignment).
B is not a subclass of anything. B is a class template. B<int> is
a class, which has no base classes. B<double> is a class that *does*
have a base class.
Is this a compiler bug or a language specification?
It is not a compiler bug.
Does anyone
suggests a portable workaround (other than avoid TS and subclass
B<int> as a nontemplate class C)?
Huh?
template<> class B<int> : public A { ...
C++ is really an interesting language... after 10+ years of daily
use, sometimes we find ourselves amused with something unexpected.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask