Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: Advanced
Sep 25, 2000

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

Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap