devxlogo

Enumerator with Zero Value and Null Pointer Constants

Enumerator with Zero Value and Null Pointer Constants

Here’s a topic that was recently discussed at the Standardization committee. Suppose we have the following enum type:

 enum Stat { good, bad};

Is it legal to use the enumerator good, whose value is zero, as a null pointer constant? Put differently, is the following declaration allowed?

 void *p = good;

After all, a null pointer constant in C++ is either 0 or 0L. The answer is “no”. This declaration isn’t allowed because the standard says that “a null pointer constant is an integral constant expression rvalue of integer type that evaluates to zero.” Let’s analyze it. A null pointer constant, e.g., NULL, 0, or 0L, must be an integral constant expression. Indeed, an enumerator is an integral constant expression. However, to qualify as a null constant expression, an identifier must also be an “rvalue of integer type”. Enumerators are rvalues but they are not of integer type. Therefore, the declaration

 void *p = good;

is illegal. There is another way to reach the same conclusion without resorting to C++ casuistry: C++ allows only one implicit conversion per expression. Therefore, had we used the literal zero, as in:

 void *p = 0;

The compiler would have implicitly converted the literal zero to type void * and assigned it to p. However, once we use good as an initializer, two implicit conversions are necessary. The first converts the enumerator to the integer zero, and the second converts the integer zero to void *. However, because C++ allows only one implicit conversion, the following declaration is invalid:

 void *p=good; // requires two implicit conversions; invalid

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist