Pointers to template functions require explicit cast

Discussion in 'C++' started by Justin Piper, Sep 20, 2006.

  1. Justin Piper

    Justin Piper Guest

    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;
    }
    Justin Piper, Sep 20, 2006
    #1
    1. Advertising

  2. 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, Sep 20, 2006
    #2
    1. Advertising

  3. Justin Piper

    Justin Piper Guest

    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, Sep 21, 2006
    #3
  4. Justin Piper

    Kai-Uwe Bux Guest

    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, Sep 21, 2006
    #4
  5. Justin Piper

    Justin Piper Guest

    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
    Justin Piper, Sep 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J.T. Conklin
    Replies:
    1
    Views:
    431
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    495
    Shezan Baig
    Jan 30, 2005
  3. Replies:
    1
    Views:
    562
    Salt_Peter
    Dec 25, 2006
  4. Alf P. Steinbach /Usenet
    Replies:
    1
    Views:
    572
    James Kanze
    Jul 27, 2010
  5. ajaybgr
    Replies:
    18
    Views:
    951
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page