Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointers to template functions require explicit cast

2 views
Skip to first unread message

Justin Piper

unread,
Sep 19, 2006, 9:20:02 PM9/19/06
to
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;
}

Stuart Redmann

unread,
Sep 20, 2006, 2:35:38 AM9/20/06
to
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

Justin Piper

unread,
Sep 21, 2006, 9:46:57 AM9/21/06
to
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.

Kai-Uwe Bux

unread,
Sep 21, 2006, 11:39:23 AM9/21/06
to
Justin Piper wrote:

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

Justin Piper

unread,
Sep 22, 2006, 6:22:33 PM9/22/06
to

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

0 new messages