example_c99.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 In the code below, it's unclear whether the call to foo(0) is meant to invoke 
 the foo(int) or foo(void*) overload. The ambiguity arises because 0 can represent 
 both a null pointer and an integer.

 Solution with nullptr (C++11):
    C++11 introduced nullptr, a new keyword that explicitly represents a null pointer 
    and is not an integer. This removes the ambiguity and makes the code clearer and safer.
*/
#include <iostream>

void foo(int value) {
    std::cout << "Integer overload: " << value << std::endl;
}

void foo(void* ptr) {
    std::cout << "Pointer overload: " << ptr << std::endl;
}

int main() {
    foo(0);  // Ambiguous: Is this calling foo(int) or foo(void*)?
    return 0;
}
Back to nullptr