Call of overloaded method with nullptr is ambiguous

  • A+

I have some overloaed methods which take some different pointer types.
Now I want to call one specific method with nullptr as a parameter.

I know that I could cast the nullptr to the specific type of pointer, the method I want it to call takes.
But I don't want to/can't cast the nullptr.

This example shoud explain what I am trying to do:

class Foo {     //some attributes }; class Bar {     //some attributes };  void myMethod (Foo*) {     //I want this method to be called } void myMethod (Bar*) {     //Not this one }  int main () {     myMethod(nullptr);              //Something like this //  myMethod(static_cast<nullptr>); //I don't want to write this.      return 0; } 

If I just call it with the nullptr I get
error: call of overloaded 'myMethod(std::nullptr_t)' is ambiguous
because the compiler doesn't know which of the methods it should call.

Is there a way to do what I want?
Like something similar to the template specialization?


You can create an overload which take std::nullptr_t as argument, and then in it call the exact function wanted (through casting):

void myMethod(std::nullptr_t) {     myMethod(static_cast<Foo*>(nullptr)); } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: