42. Overload Resolution Trap

#include <iostream>
using namespace std;

void foo(int x) {
    cout << "int overload\n";
}

void foo(int* p) {
    cout << "int* overload\n";
}

int main() {

   foo(nullptr); // nullptr: actually a empty pointer
   cout << 3.3 + NULL;  // NULL: macro defined as 0 
   
   return 0;
}


Solution Details

  • NULL is usually defined as 0 (an integer literal).  
  • nullptr is a special type (std::nullptr_t) that converts to pointer types but not to integers, so foo(nullptr) selects foo(int*) unambiguously.
     

In simple words:

  • NULL is like just writing the number 0, so the compiler thinks you meant the integer.
  • nullptr means “this is an empty pointer,” so the compiler picks the pointer version.
     


Significance for Embedded Developers

  • Driver APIs often have overloads taking an integer config value or a pointer to a buffer/structure.
     
  • Using nullptr instead of NULL avoids calling the wrong overload, which can cause subtle bugs (e.g., passing 0 and hitting a numeric route instead of a pointer route).
     
  • Prefer nullptr for clarity and type safety in firmware code.
     
Loading...

Input

Expected Output

int* overload 3.3