How to define nullptr for supporting both C++03 and C++11?

Amir Saniyan picture Amir Saniyan · May 8, 2012 · Viewed 10.4k times · Source

Possible Duplicate:
“Backporting” nullptr to C++-pre-C++0x programs

How to define nullptr for supporting both C++03 and C++11?

Does below code is compiled with both C++03 and C++11 compiles without change the meaning of nullptr in C++11 compiler?

#include <cstddef>

#if !defined(nullptr)
#define nullptr NULL
#endif

Answer

Morwenn picture Morwenn · May 8, 2012

In C++11, nullptr is of type nullptr_t. One of its big advantages compared to NULL (or to 0 would say Bjarne since he does not like macros) is that between these two functions:

void foo(char*);
void foo(int);

foo(nullptr) will call the char* overload but foo(NULL) will call the int overload. So your solution may work in most of the cases, but you may have errors with functions overload.