V
vilarneto
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> {
};
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).
Is this a compiler bug or a language specification? Does anyone
suggests a portable workaround (other than avoid TS and subclass
B<int> as a nontemplate class C)?
C++ is really an interesting language... after 10+ years of daily
use, sometimes we find ourselves amused with something unexpected.
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> {
};
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).
Is this a compiler bug or a language specification? Does anyone
suggests a portable workaround (other than avoid TS and subclass
B<int> as a nontemplate class C)?
C++ is really an interesting language... after 10+ years of daily
use, sometimes we find ourselves amused with something unexpected.