Template Function Pointers + Inherited Return Types

I

infogoogle

Hello,

i'm having problems with the type of a template function:

This code:

class A {};
class B : A {};

template<class T> B* fnull() { return 0; };

typedef A *(*afun)() ;
afun nn = fnull<int>;

produces the following error

pointer-to-template-instantiation.cpp:8: error: no matches converting
function `null' to type `class A*(*)()'
pointer-to-template-instantiation.cpp:4: error: candidates are:
template<classT> B* null()

When I change the return type to "A*" , or when I remove the
template<...>, this error disappears, so it seems to be the combination
of the two somehow. Any suggestions?

--
Johannes Leitner
Software Engineering Group
http://www.inf.uni-konstanz.de/~leitner

University of Constance Phone: (+49 7531)
88-2188
78457 Konstanz Fax: (+49 7531)
88-3577
Germany Office: Building E, Room
222
 
V

Victor Bazarov

i'm having problems with the type of a template function:

This code:

class A {};
class B : A {};

template<class T> B* fnull() { return 0; };

typedef A *(*afun)() ;
afun nn = fnull<int>;

produces the following error

pointer-to-template-instantiation.cpp:8: error: no matches converting
function `null' to type `class A*(*)()'
pointer-to-template-instantiation.cpp:4: error: candidates are:
template<classT> B* null()

There seems to be a discrepancy between your code and your error
message. What is the name of your function?
When I change the return type to "A*" , or when I remove the
template<...>, this error disappears, so it seems to be the
combination of the two somehow. Any suggestions?

Are you attempting to utilise "covariant" return types with non-virtual
non-member functions? Generally speaking, when the template needs to
be instantiated, all templates are looked at, and the compiler tries to
deduce the template argument. In your case you give the compiler
the type argument explicitly, but the function signature does not match
the pointer type to which the function is supposed to be converted: the
return value type is different. The fact that B and A are related
notwithstanding. So, the compiler cannot instantiate the template and
no function is found.

As to why it manages to convert a plain (non-template) function, I am
not sure, probably the return value type is not checked in that case.

V
 
K

Kai-Uwe Bux

Hello,

i'm having problems with the type of a template function:

This code:

class A {};
class B : A {};

template<class T> B* fnull() { return 0; };

typedef A *(*afun)() ;
afun nn = fnull<int>;

produces the following error

pointer-to-template-instantiation.cpp:8: error: no matches converting
function `null' to type `class A*(*)()'
pointer-to-template-instantiation.cpp:4: error: candidates are:
template<classT> B* null()

When I change the return type to "A*" ,

the error disappears: now the function types match.
or when I remove the template<...>, this error disappears,

no it does not:

class A {};
class B : A {};

B* fnull() { return 0; };

typedef A*(*afun)() ;

int main ( void ) {
afun nn = fnull;
}

error: invalid conversion from 'B* (*)()' to 'A* (*)()'

so it seems to be the combination
of the two somehow. Any suggestions?

For function types to be compatible, the signatures including return types
have to agree. Make them match.



Best

Kai-Uwe Bux
 
I

infogoogle

Victor said:
In your case you give the compiler
the type argument explicitly, but the function signature does not match
the pointer type to which the function is supposed to be converted: the
return value type is different. The fact that B and A are related
notwithstanding. So, the compiler cannot instantiate the template and
no function is found.

Thanks! I misinterpreted the error message, I thought the template was
instantiated and _then_ types didnt match. I didn't know that templates
where instantiated according to the type that is required from context.
When I add explicite type info, it works:

afun nn = (afun) (B*(*)()) fnull<int>;

Many Thanks!

--
Johannes Leitner
Software Engineering Group
http://www.inf.uni-konstanz.de/~leitner

University of Constance Phone: (+49 7531)
88-2188
78457 Konstanz Fax: (+49 7531)
88-3577
Germany Office: Building E, Room
222
 
V

Victor Bazarov

Thanks! I misinterpreted the error message, I thought the template
was instantiated and _then_ types didnt match. I didn't know that
templates where instantiated according to the type that is required
from context. When I add explicite type info, it works:

afun nn = (afun) (B*(*)()) fnull<int>;

"Works"? You stepped on the value that the compiler gives with your
C-style cast. It's very dangerous.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top