On May 13, 4:50 pm, MikhailGorbac...@gmail.com wrote:
I'm having a problem getting derived types to be inferred as valid
parameters by a function template.
template <typename T>
struct Base {}
template<typename T>
struct Derived : public Base<T{}
template <typename T>
void fn(Base<T>* b) { }
// strictly not want you want but might do
template <typename X>
void fn(X* x)
{
Base<T>* b( x );
....
}
Or add this method - at least you know that it's only the derived ones
that you're getting
template <typename T>
void fn(Derived<T>* x)
{
Base<T* b = x;
fn( b );
}
Or - hack but does the job - breaks some non-compliant compilers
// strictly not want you want but might do
template <typename X>
void fn(X* x, Convertible<X *, Base<T*>::type = 0 )
{
Base<T>* b( x );
fn(b);
}
where Convertible is a special template class to determine if the type
of X is one of the types you allow be used - it's probably overkill
for this case.
>
When I call fn with a Derived<int>* gcc gives me the error "no
matching function for call to..."
Is there any way to get the function template to accept derived
objects?
Thanks,
Alex Rubinsteyn