C++98/03中非静态数据成员初始化的GCC兼容问题咨询
void *x = NULL; but Rejects void *y = 0; in C++98/03 Great question! Let's unpack this inconsistent behavior in GCC 4.8.1 when working with C++98/03 standards, where non-static data member initializers (NSDMIs) aren't supposed to exist at all.
Background Recap
First, a quick reminder: C98 and C03 do not support NSDMIs—this feature was introduced in C++11. So theoretically, both lines of your code should trigger a compiler error. But GCC 4.8.1 has a quirky exception here, and it all comes down to how it handles NULL vs. the integer 0.
The Root Cause
What
NULLmeans in GCC 4.8.1
In older GCC versions like 4.8.1,NULLis typically defined as__null—a compiler-builtin keyword specifically designed to represent a null pointer constant, rather than the integer0or(void*)0.Partial C++11 Support in GCC 4.8.x
GCC 4.8.x started adding partial support for C11 features even when compiling in C98/03 mode as a convenience. For__null(akaNULL), the compiler seems to treat the in-class initialization as a special case of null pointer assignment, mistakenly allowing it even though NSDMIs aren't part of C++98/03.Why
0gets rejected
The integer0is just a regular integer constant. GCC correctly enforces the C++98/03 standard here: since NSDMIs aren't allowed, initializing a non-static member with0triggers the expected error mentioning "non-static data member initializer". The same applies to(void*)0—even though it's a null pointer constant, it's not the compiler-specific__null, so GCC sticks to the standard and rejects it.
Quick Verification Check
If you look at the definition of NULL in GCC 4.8.1's <cstddef> header, you'll see something like:
#define NULL __null
This builtin __null is what's causing the special treatment that bypasses the C++98/03 NSDMI restriction.
In short, this is a case of GCC's early, partial C11 support creating an inconsistency in its C98/03 compliance—handling the compiler-specific null pointer constant differently from regular integer or casted null values.
内容的提问来源于stack exchange,提问作者mariusz_latarnik01

