Does D have an equivalent to C++ nullptr_t?

204 views Asked by At

in C++, I can do this:

void func(void *something) {
    cout << "something" << endl;
}

void func(nullptr_t) {
    cout << "nullptr_t" << endl;
}

int main() {
    int nothing = 5;
    func(&nothing);
    func(nullptr);
    return 0;
}

which outputs:

something
nullptr_t

so I can special case nullptr_t.

In D, I have two functions:

void func(void* pod) {
    cout << "pod" << endl;
}

void func(Object obj) {
    cout << "Object" << endl;
}

So how do I resolve the ambiguity when calling func(null)?

1

There are 1 answers

1
Adam D. Ruppe On BEST ANSWER
void func(typeof(null)) {}

The null literal is a special type in D, which you can specifically get with the typeof operator.

Note that this will only catch a null literal - it will not catch a null Object or null pointer variable.