dcsimg
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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Tackling the Conundrums of Constant Expressions-2 : Page 2


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


In the following cases, C++ requires expressions that evaluate to an integral constant expression: Contrary to popular conception, not every const variable of an integral type is a constant expression. Consider the following two examples:

struct C { inline static int getval() {return 4;} }; const int MAX=1024; const int MIN=C::getval();

Both MAX and MIN are constants, eg., the program can't modify their values. However, there is a substantial difference between them. MAX is a constant integral expression. As such, it can be used in an array declaration, case labels, etc.:

char buff[MAX]; int sizes=getsizes(); switch (sizes) { case MAX: //.. break; default: //.. break; };

What about MIN? Syntactically speaking, it's a const object with an initializer, just as MAX is. However, if you try to use MIN in places where an integral constant expression is required, your compiler will complain:


struct bitpattern { signed nibble: MIN;//error: constant expression required unsigned octet: 8; }; enum SIZES { S=MIN, //error: constant expression required L=512, XL=MAX //fine };

In the examples above, a compilation error reveals that MIN isn't a constant expression. In fact, this is your litmus test: use a constant as an enumerator's initializer to check whether it's a valid constant expression.



Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date