C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Function template type inference doesn't work with inheritan

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sun May 13, 2007 9:11 am    Post subject: Function template type inference doesn't work with inheritan Reply with quote



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) { }

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
Back to top
Gianni Mariani
Guest





PostPosted: Sun May 13, 2007 9:11 am    Post subject: Re: Function template type inference doesn't work with inher Reply with quote



On May 13, 4:50 pm, MikhailGorbac...@gmail.com wrote:
Quote:
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.

Quote:

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.