473,507 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template Function Pointers + Inherited Return Types

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
4 2273
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4561
by: Surya Kiran | last post by:
Hi all, I've written a function template. say template <class T> fn (T var) { ... } Is there any way, from within the function, can we check what type of argument we've passed on to the...
9
2296
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
8
1946
by: Thomas Heller | last post by:
I need to convert C preprocessor definitions into python code. The definitions are dumped out of gccxml (see http://www.gccxml.org) , running over the windows header files (for example). This...
0
1094
by: gao_bolin | last post by:
I have a library that has a class Signal<T> whose data type is templated. The class comes with a whole bunch of functions and objects to do various processing on the signal. What I would like to...
0
1622
by: ckhoge | last post by:
Hi, Consider this code fragment, based on the article "Using Chains to Free Library Code" from the July 2005 issue of C/C++ Users Journal: -- begin listing -- template <class T> struct Foo...
3
1953
by: a | last post by:
Hi, I'm trying to create a helper class that will allow me to use a pointer or reference to a class without knowing if it is a pointer or reference. Conceptual example: //******************...
2
1877
by: Dom Jackson | last post by:
Hello - I have a problem where I need to test some numeric code using a variety of built-in integer types: obj_type1 = obj_type2 OP obj_type3; // is obj_type1 correct? If I test with 10...
9
3446
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. ...
6
391
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.