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(¬hing);
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)
?
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.