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

Pointers to template functions require explicit cast

P: n/a
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
Share this Question
Share on Google+
4 Replies


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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.