473,399 Members | 2,278 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,399 software developers and data experts.

Pointers to template functions require explicit cast

I was toying with using function pointers to implement a state machine,
and ran into trouble when I tried to use a template to specify the type
of one of the arguments. I was able to determine that I need to
explicitly cast a pointer to a template function to the correct type,
and I was hoping someone could explain why this is necessary. I have
included a minimal example exhibiting the problem below. It was
compiled using the MinGW port of g++ 3.4.2 on W2kSP4.

#include <iostream>

struct evaluator {
bool (*eval)(evaluator&);
};

template <typename T>
bool stop(T &e) { return true; }

bool eval(evaluator &e) { return true; }

int main() {
typedef bool (*evalf)(evaluator&);
struct evaluator e = { stop<evaluator};

// error: assuming cast to type 'bool (*)(evaluator&)' from
// overloaded function
std::cout << (e.eval == stop<evaluator>) << '\n';

// ok--eval is not templated
std::cout << (e.eval == eval) << '\n';

// ok--explicitly cast to correct type
std::cout << (e.eval == static_cast<evalf>(stop<evaluator>)) <<
'\n';

return 0;
}

Sep 20 '06 #1
4 2667
Justin Piper wrote:
I was toying with using function pointers to implement a state machine,
and ran into trouble when I tried to use a template to specify the type
of one of the arguments. I was able to determine that I need to
explicitly cast a pointer to a template function to the correct type,
and I was hoping someone could explain why this is necessary. I have
included a minimal example exhibiting the problem below. It was
compiled using the MinGW port of g++ 3.4.2 on W2kSP4.

#include <iostream>

struct evaluator {
bool (*eval)(evaluator&);
};

template <typename T>
bool stop(T &e) { return true; }

bool eval(evaluator &e) { return true; }

int main() {
typedef bool (*evalf)(evaluator&);
struct evaluator e = { stop<evaluator};

// error: assuming cast to type 'bool (*)(evaluator&)' from
// overloaded function
std::cout << (e.eval == stop<evaluator>) << '\n';
There is no problem here for Visual C 6.0. The output of this programme
is 1 0 1 as I would have expected.
// ok--eval is not templated
std::cout << (e.eval == eval) << '\n';

// ok--explicitly cast to correct type
std::cout << (e.eval == static_cast<evalf>(stop<evaluator>)) <<
'\n';

return 0;
}
Regards,
Stuart
Sep 20 '06 #2
Stuart Redmann wrote:
Justin Piper wrote:
I was toying with using function pointers to implement a state machine,
and ran into trouble when I tried to use a template to specify the type
of one of the arguments. I was able to determine that I need to
explicitly cast a pointer to a template function to the correct type,
and I was hoping someone could explain why this is necessary. I have
included a minimal example exhibiting the problem below. It was
compiled using the MinGW port of g++ 3.4.2 on W2kSP4.

#include <iostream>

struct evaluator {
bool (*eval)(evaluator&);
};

template <typename T>
bool stop(T &e) { return true; }

bool eval(evaluator &e) { return true; }

int main() {
typedef bool (*evalf)(evaluator&);
struct evaluator e = { stop<evaluator};

// error: assuming cast to type 'bool (*)(evaluator&)' from
// overloaded function
std::cout << (e.eval == stop<evaluator>) << '\n';

There is no problem here for Visual C 6.0. The output of this programme
is 1 0 1 as I would have expected.
Thanks, Stuart. It seems to work under VC7 as well. It seems like this
should work without casting, so I think I'll try to find someone with
g++ 4.1 and file a bug if it exhibits the same problem.

Sep 21 '06 #3
Justin Piper wrote:
Stuart Redmann wrote:
>Justin Piper wrote:
I was toying with using function pointers to implement a state machine,
and ran into trouble when I tried to use a template to specify the type
of one of the arguments. I was able to determine that I need to
explicitly cast a pointer to a template function to the correct type,
and I was hoping someone could explain why this is necessary. I have
included a minimal example exhibiting the problem below. It was
compiled using the MinGW port of g++ 3.4.2 on W2kSP4.

#include <iostream>

struct evaluator {
bool (*eval)(evaluator&);
};

template <typename T>
bool stop(T &e) { return true; }

bool eval(evaluator &e) { return true; }

int main() {
typedef bool (*evalf)(evaluator&);
struct evaluator e = { stop<evaluator};

// error: assuming cast to type 'bool (*)(evaluator&)' from
// overloaded function
std::cout << (e.eval == stop<evaluator>) << '\n';

There is no problem here for Visual C 6.0. The output of this programme
is 1 0 1 as I would have expected.

Thanks, Stuart. It seems to work under VC7 as well. It seems like this
should work without casting, so I think I'll try to find someone with
g++ 4.1 and file a bug if it exhibits the same problem.
I ran it on g++4.1.1 and got the error:
001.cc: In function 'int main()':
001.cc:24: error: assuming cast to type 'bool (*)(evaluator&)' from
overloaded function
Best

Kai-Uwe Bux
Sep 21 '06 #4
Kai-Uwe Bux wrote:
Justin Piper wrote:
Stuart Redmann wrote:
Justin Piper wrote:
I was toying with using function pointers to implement a state machine,
and ran into trouble when I tried to use a template to specify the type
of one of the arguments. I was able to determine that I need to
explicitly cast a pointer to a template function to the correct type,
and I was hoping someone could explain why this is necessary. I have
included a minimal example exhibiting the problem below. It was
compiled using the MinGW port of g++ 3.4.2 on W2kSP4.

#include <iostream>

struct evaluator {
bool (*eval)(evaluator&);
};

template <typename T>
bool stop(T &e) { return true; }

bool eval(evaluator &e) { return true; }

int main() {
typedef bool (*evalf)(evaluator&);
struct evaluator e = { stop<evaluator};

// error: assuming cast to type 'bool (*)(evaluator&)' from
// overloaded function
std::cout << (e.eval == stop<evaluator>) << '\n';

There is no problem here for Visual C 6.0. The output of this programme
is 1 0 1 as I would have expected.
Thanks, Stuart. It seems to work under VC7 as well. It seems like this
should work without casting, so I think I'll try to find someone with
g++ 4.1 and file a bug if it exhibits the same problem.

I ran it on g++4.1.1 and got the error:
001.cc: In function 'int main()':
001.cc:24: error: assuming cast to type 'bool (*)(evaluator&)' from
overloaded function
Thanks for your help. I've reported the problem as bug 29187 to the GCC
team.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29187

Sep 22 '06 #5

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

Similar topics

2
by: Patchwork | last post by:
Hi Everyone, I have a design-/implementation-related question for you all. In practical usage of C++, especially when processing image or audio data with 3rd party libraries, there often...
3
by: red floyd | last post by:
I got an error by using std::fill to set an array of pointers to 0. e.g.: class XXX; XXX* v; std::fill(v, v+30, 0); // <-- ERROR -- cant' match template type I have to either explicitly...
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
7
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
5
by: Steve Schlesinger | last post by:
Suppose you have class A {}; class B {} template<class T> void SomeFcn( T t ) {…}; A a; B b;
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
15
by: bwaichu | last post by:
I'm struggling with the concept of function pointers. I understand that a function pointer contains the memory address of the function. But I'm not sure how they are effectively used, and I'm not...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.