Pointer to templatized, overloaded member functions

M

mrstephengross

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 ([email protected])
 
V

Victor Bazarov

mrstephengross said:
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
 
M

mrstephengross

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
 

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,054
Latest member
TrimKetoBoost

Latest Threads

Top