Skip to main content
gamedev.net
🔒 Locked

Ambiguous pointer to overloaded method

Started by wild_pointer Sep 29, 2006 at 9:37 AM 2 replies 850+ views
Original Post
wild_pointer
wild_pointer
I have the following class declaration:

class A
{
  void B(bool);
  bool B();
};
And elsewhere I try to take a pointer to A::B

std::mem_fun(&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 :)).
[size=2]
Skizz
Skizz
You need to use:

std::mem_fun (&A::B)

Skizz
Fruny
Fruny
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(&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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.