Type Qualifiers


  • Linux Distribution
  • g++
  • any text editor

My Setup:

  • Debian 10
  • g++ version 6.3.0
  • pluma

In my “Classes” tutorial, I talked about const briefly. In this tutorial, I go over “Type Qualifiers”. What is a type qualifier? A type qualifier is a way of expressing additional information about the value through the type system. In C++, there are currently 3 type qualifiers:

1. Const

Let us start with the const qualifier. Const is short for constant. Variables declared as const are usually placed in read-only memory by the compiler. If the variable is never used within a program it may be taken out completely. As a result, the data can’t be manipulated.

const char c = 'B'; //const char type , read only
c = 'C'; // error: type of c is const-qualified

float f = 9.37f;
const float *cf = &x; //assigning a non-const to const pointer
*cf = 5.39f; // error: the type of the lvalue *cf is const-qualified

Now there are ways to modify variables that are currently const, such as a const_cast as such:

const char c = 'B';
char *foo = const_cast<char*>(&c);
2. Volatile

The volatile type qualifier is almost on the completely opposite spectrum. Declaring a variable volatile tells the compiler that the variable is modifiable externally through a signal or other processes. As a result, this stops any optimizations from happening to a variable. This is usually done when you read from a port. Since the data can constantly be changing the variable instead of reading once and using the temporary variable, as optimizations may do, it will constantly read the port. This is also usually suitable for communicating with a signal handler, but not with another thread of execution.

3. Const Volatile

A const volatile variable acts as both a const and volatile qualified


This is not getting a number because I am not sure if this is even considered a type qualifier. However, if you need to modify a member of a const object , you may use the mutable keyword. The cppreference site has a really good example of this:

class X {
  mutable const int* p;         // OK
  mutable int* const q;         // ill-formed

class ThreadsafeCounter {
  mutable std::mutex m; // The "M&M rule": mutable and mutex go together
  int data = 0;
  int get() const {
    std::lock_guard<std::mutex> lk(m);
    return data;
  void inc() {
    std::lock_guard<std::mutex> lk(m);

For more information on Type Qualifiers you can check out

6 thoughts on “Type Qualifiers

  1. /*prg to change the value of a const variable */
    int main()
    const int a = 10;
    int *ptr = &a;
    *ptr = 77;
    printf(“a = %d\n”,a);
    return 0;

    1. that isnt const :
      prog.c:6:12: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
      int *ptr = &a;

      you essentially take out const when you do that. That is also C, in c++ you would get:
      prog.cpp:6:12: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
      int *ptr = &a;

      1. Yes we will get warning in “C” but not error.
        Please observe the value of “a” variable.It is modified.

      2. The variable a Is not const, the compiler took that away from the variable prior to the compilation. The compiler tells you that when it says: initialization discards ‘const’ qualifier.

Leave a Reply