Pointer to templatized, overloaded member functions

Discussion in 'C++' started by mrstephengross, Jul 2, 2007.

  1. Hi folks! I'm trying to create a function pointer to a templatized,
    static, overloaded member function. My class (Mgr) has two functions
    with the same name: "go". The first takes a T reference (T is a
    template argument), and an int. The second takes a T reference and two
    ints.

    Here's the code:

    =================
    #include <iostream>

    struct Mgr
    {
    template<class T> static void go(T &, int);

    template<class T> static void go(T &, int, int);
    };

    int main()
    {
    void (Mgr::*foo) (char &, int) = Mgr::go<char>;

    return 0;
    }
    ======== EOF =======

    Using gcc 3.4.6, I get the following compile error:

    test.cpp: In function `int main()':
    test.cpp:12: error: no matches converting function `go' to type `void
    (struct Mgr::*)(char&, int)'
    test.cpp:5: error: candidates are: template<class T> static void
    Mgr::go(T&, int)
    test.cpp:7: error: template<class T> static void
    Mgr::go(T&, int, int)

    Apparently, g++ can't figure out which version of Mgr::go(...) I'm
    talking about. Is there any way to disambiguate in this case?

    Thanks,
    --Steve ()
     
    mrstephengross, Jul 2, 2007
    #1
    1. Advertising

  2. mrstephengross wrote:
    > Hi folks! I'm trying to create a function pointer to a templatized,
    > static, overloaded member function. My class (Mgr) has two functions
    > with the same name: "go". The first takes a T reference (T is a
    > template argument), and an int. The second takes a T reference and two
    > ints.
    >
    > Here's the code:
    >
    > =================
    > #include <iostream>
    >
    > struct Mgr
    > {
    > template<class T> static void go(T &, int);
    >
    > template<class T> static void go(T &, int, int);
    > };
    >
    > int main()
    > {
    > void (Mgr::*foo) (char &, int) = Mgr::go<char>;
    >
    > return 0;
    > }
    > ======== EOF =======
    >
    > Using gcc 3.4.6, I get the following compile error:
    >
    > test.cpp: In function `int main()':
    > test.cpp:12: error: no matches converting function `go' to type `void
    > (struct Mgr::*)(char&, int)'
    > test.cpp:5: error: candidates are: template<class T> static void
    > Mgr::go(T&, int)
    > test.cpp:7: error: template<class T> static void
    > Mgr::go(T&, int, int)
    >
    > Apparently, g++ can't figure out which version of Mgr::go(...) I'm
    > talking about. Is there any way to disambiguate in this case?


    There is nothing to disambiguate. You're trying to assign [a pointer
    to] a *static* member function to a pointer-to-member variable. Those
    are incompatible. You need to make 'foo' a regular pointer-to-function:

    void (*foo)(char&, int) = &Mgr::go<char>;

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 2, 2007
    #2
    1. Advertising

  3. > There is nothing to disambiguate. You're trying to assign [a pointer
    > to] a *static* member function to a pointer-to-member variable. Those
    > are incompatible. You need to make 'foo' a regular pointer-to-function:
    >
    > void (*foo)(char&, int) = &Mgr::go<char>;


    Aha!

    Thanks,
    --Steve
     
    mrstephengross, Jul 2, 2007
    #3
    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. ma740988
    Replies:
    3
    Views:
    383
    Cy Edmunds
    Nov 6, 2004
  2. mrstephengross
    Replies:
    5
    Views:
    598
    Larry I Smith
    May 18, 2005
  3. StephQ
    Replies:
    1
    Views:
    323
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Stephen Howe
    Replies:
    2
    Views:
    291
    Stephen Howe
    Nov 6, 2012
Loading...

Share This Page