#include <iostream>
using namespace std;
void foo(int x) {
cout << "int overload\n";
}
void foo(int* p) {
cout << "int* overload\n";
}
int main() {
foo(0);
foo(nullptr);
return 0;
}
Explanation & Logic Summary:
0 is an integer literal, so overload resolution selects foo(int).nullptr has type std::nullptr_t and converts only to pointer types, so foo(int*) is selected.- This behavior is guaranteed by the C++ standard, unlike
NULL.
Firmware Relevance & Real-World Context:
- Firmware APIs often overload functions for:
- Scalar configuration values
- Pointer-based buffers or structures
- Using
nullptr instead of integer literals or macros prevents:- Accidental selection of the wrong overload
- Subtle runtime bugs caused by incorrect API paths
- This practice improves type safety, readability, and portability in embedded C++ code.