devxlogo

Idempotent Type Qualifiers

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.

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