By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Template Function Pointers + Inherited Return Types

P: n/a
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<classTB* 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

Aug 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
in********@logopolis.de wrote:
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<classTB* 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[s]. 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 11 '06 #2

P: n/a
in********@logopolis.de wrote:
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<classTB* 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
Aug 11 '06 #3

P: n/a
Victor Bazarov wrote:
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

Aug 11 '06 #4

P: n/a
in********@logopolis.de wrote:
Victor Bazarov wrote:
>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>;
"Works"? You stepped on the value that the compiler gives with your
C-style cast. It's very dangerous.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.