 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun May 13, 2007 9:11 am Post subject: Function template type inference doesn't work with inheritan |
|
|
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
|
Posted: Sun May 13, 2007 9:11 am Post subject: Re: Function template type inference doesn't work with inher |
|
|
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 |
|
 |
|
|
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
|
|