Sign in to follow this  

Ambiguous pointer to overloaded method

Recommended Posts

I have the following class declaration:
class A
  void B(bool);
  bool B();
And elsewhere I try to take a pointer to A::B
This results in a compiler error. Is there any way to tell the compiler which version I want? Currently, I'm using a local class to wrap the bool A::B() call in a functor but I was hoping there was a better way (besides renaming my methods :)).

Share this post

Link to post
Share on other sites
The compiler selects the function according to the type of the pointer you are assigning the expression to. For example:

void (A::*foo)(bool) = &A::B; selects void A::B(bool)
bool (A::*foo)() = &A::B; selects bool A::B()

Since mem_fun is a function template, you can normally use parameter type deduction but here, the expression &A::B can have two different types, two different values. It is thus unsurprising that the parameter type deduction mechanism would fail. You need to tell the compiler which one to use, either, as Skizz points out by forcing one version or another of mem_fun or by casting the expression results to the appropriate pointer type: e.g. std::mem_fun(static_cast<bool (A::*)()>(&A::B)).

Skizz's approach is the easiest solution in this case -- but it might not be in another (e.g. when more type manipulations happen in the template).

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this