In C23, the nullptr keyword got standardized. I would prefer to use nullptr instead of NULL prior to C23 too because it means that I could write code which compiles in:
- C, prior to C23
- C, since C23
- C++
I could simply use NULL in both languages and every C standard, but that would be highly unusual in C++ code, and nullptr is becoming the norm in both languages anyway.
As far as I know, you are not allowed to use #define to replace keywords in the language, and that may cause problems when defining a compatibility macro. Basically, I need:
// only if this is neither C++ nor C23
#define nullptr /* something */
How can I properly define this macro?
Some things of note:
__STDC__and__STDC_VERSION__were added in "C95" (the addendum to ISO C 9899:1990).nullptrusing pre-processor conditionals because it is not a macro.stddef.hor equivalent header even when using a conforming C23 compiler so don't assume that this one is #included.-std=c2xwhich does containnullptrbut sets__STDC_VERSION__to a placeholder value 202000L.nullptrsince it was added in C++11.Therefore the macro checks should look something like this: