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
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Mar 23, 2000

Idempotent Type Qualifiers

The type qualifiers const and volatile are idempotent. This means that if a type qualifier is included indirectly several times in a type specification (e.g., through a typedef or a template parameter), it's treated as if it appeared only once. For example:

 
  typedef const int CI;

The typedef CI is a synonym for "const int". Now suppose we declare another typedef based on CI:

 
  typedef const CI CCI;  

CCI is a synonym for "const CI", which is actually "const const int". The compiler ignores the redundant const and treats CCI as a synonym for "const int". For example:

 
  int f(CCI n); // fine, f takes a const int

Remember that this rule applies to indirect inclusion of a type qualifier; direct repetitions are illegal:

 
  void f(const const int); // ill-formed 

In C99, this restriction was relaxed and even direct repetitions are allowed.

Danny Kalev
 
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