Pointers to template functions require explicit cast

J

Justin Piper

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;
}
 
S

Stuart Redmann

Justin said:
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
 
J

Justin Piper

Stuart said:
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.
 
K

Kai-Uwe Bux

Justin said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top