473,321 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Function template type inference doesn't work with inheritance?

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

May 13 '07 #1
2 1715
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

May 13 '07 #2
Mi**************@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) { }

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<intd;
foo(&d);

return 0;
}

Regards,

Zeppe
May 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ivan | last post by:
How (at compile time) can one determine whether some class implements a particular member function? Name only is sufficient, full signature match nice but not required. The application is a...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
4
by: Vijai Kalyan | last post by:
I was decomposing a task into different policies. Essentially, there is a general option obtained from a server and user options obtained from configuration variables. The two options are...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
4
by: Israel | last post by:
Can someone explain to me why I get a compiler error with the following code on the line obj.DoSomething()? but if cast the object I get no error. The error I get is: Cannot access protected...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.