Re: g++ and subclass template specialization
* vilarneto@gmail.com:
On 9 abr, 21:10, "Alf P. Steinbach" <a...@start.no> wrote:
A specialization doesn't inherit anything from the base template.
...which makes me wonder: is that possible to make specialized B<int>
a subclass of B<T=int>?
There is no such thing as "B<T=int>".
Since they are unrelated classes, as you said before, that kind of
subclassing should pose no problems... but I can't think of the
syntax for that.
I've tried an intermediate typedef, but it generated a compiler
error... OK, I recognize that this is confusing even for humans,
because it smells like recursion:
typedef B<int> C;
template<>
class B<int> : public C { // error: invalid use of undefined type
`class C'
};
You'd need a declaration of B to make the compiler accept the typedef,
and then the specialization would effectively be
class B<int>: public B<int> { ... }
and a class can't inherit from itself.
However, you can do
#include <iostream>
#include <ostream>
template< typename T > struct S;
template<> struct S<double> { double d; S():d(1){} };
template<> struct S<int>: S<double> { int i; S():i(2){} };
int main()
{
S<int> o;
std::cout << o.d << " " << o.i << std::endl;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?