8

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?

hellow
  • 12,430
  • 7
  • 56
  • 79
Melvin S.
  • 83
  • 5
  • I forgot about this question and didn't close nor respond to it because the issue was fixed. Turns out I could use `myMethod(static_cast(nullptr));` after all. – Melvin S. Aug 27 '22 at 11:04
  • It will be better if you could accept one of the answers. – Hari Jun 18 '23 at 09:17

4 Answers4

7

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));
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
5

You can create pointer of Foo and Bar and let both point to nullptr. Now you can call a overloaded function by passing pointer variable as argument.

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 () {
    Foo* foo=nullptr;
    Bar* bar=nullptr;
    myMethod(foo);              //This will call myMethod(Foo*)

    return 0;
}
rajenpandit
  • 1,265
  • 1
  • 15
  • 21
4

Some programmer dude has a good suggestion, but you could also add a default parameter to one of your methods if you were happy to call it without passing nullptr, like so:

void myMethod (Foo* = nullptr) {}
void myMethod (Bar*) {}

int main () {
    myMethod();
}
Tas
  • 7,023
  • 3
  • 36
  • 51
4

Like something similar to the template specialization?

If that means that you wish to specify the target class on a case by case basis, you can turn the overload in @Some programmer dude's answer into a template.

template<class C>
void myMethod(std::nullptr_t) {
    myMethod(static_cast<C*>(nullptr));
}

Now you can use a simple template name to call the overload you want

myMethod<Foo>(nullptr); // What you want now.
myMethod<Bar>(nullptr); // What you may want at another point.
myMethod<Baz>(nullptr); // What you may want sometime in the future,
                        // after adding another overload.
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • why pass the `nullptr` if anyhow thats the only allowed value for that overload? I mean `myMethod();` would also do it. Well...might be that there are situations where this makes sense – 463035818_is_not_an_ai Aug 28 '18 at 11:50
  • @user463035818 - It's not the only allowed value. Common other options include `0`, `NULL`, and probably other integral constant expressions with the value 0. Though I suppose it does look funny. I'll keep it, let it be just one step in refactoring. – StoryTeller - Unslander Monica Aug 28 '18 at 11:52