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;
} 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
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.
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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*);...
|
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)...
|
by: Steve Schlesinger |
last post by:
Suppose you have
class A {};
class B {}
template<class T> void SomeFcn( T t ) {…};
A a;
B b;
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |