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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: