Can someone please help me to understand why this code compiles:
class A {};
class B : public A {};
void foo( A ) {}
int main() {
B b;
foo( b );
}
but this not:
class A {};
class B : public A {};
template< typename T >
class wrapper {};
void foo( wrapper<A> ) {}
int main() {
wrapper<B> b;
foo( b );
}
The only difference in the second code (i.e., the code that does not compile) is that the classes A
and B
are wrapped as template parameters in the class wrapper
; the wrapping, surprisingly, seems to hinder the conversion from B
to A
.
It would also be great if someone can help me to fix the second code.