Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function template type inference doesn't work with inheritance?

Reply
Thread Tools

Function template type inference doesn't work with inheritance?

 
 
MikhailGorbachev@gmail.com
Guest
Posts: n/a
 
      05-13-2007
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

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      05-13-2007
On May 13, 4:50 pm, (E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Zeppe
Guest
Posts: n/a
 
      05-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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) { }
>
> 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?


FAQ 5.8 http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
If you don't post compilable code, usually we can't help.
(btw, it seems that the section 5 of the faq is by far the most quoted,
isn't it? )

The answer is that it should work. The following compile correctly in
both vc8 and gcc 3.4 (the ones that I have now).


template <class T>
class Base
{ };

template <class T>
class Derived
: public Base<T>
{ };

template <class T>
void foo(const Base<T>*)
{ }

int main()
{
Derived<int> d;
foo(&d);

return 0;
}



Regards,

Zeppe
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Localized Type Inference of Atomic Types in Python Brett C. Python 3 02-15-2014 02:38 PM
Template Type Inference of Reference Andrew Tomazos C++ 7 05-08-2009 12:48 PM
Type inference with mutually recursive generic types Niklas Matthies Java 0 10-24-2006 01:58 PM
Type inference, lessons learned from Vyper Dave Benjamin Python 0 02-03-2005 04:03 PM
Boo: Statically typed Python with type inference is done (well, kinda ;-) Ville Vainio Python 2 07-16-2004 05:21 PM



Advertisments