Search code examples
c++constantslanguage-lawyervolatile

Why does the C++ Standard define a partial ordering for cv qualifiers?


The C++20 standard (checked on N4892) states:

There is a partial ordering on cv-qualifiers, so that a type can be said to be more cv-qualified than another. Table 13 shows the relations that constitute this ordering.

6.8.4.5.

With table 13:

no cv-qualifier < const
no cv-qualifier < volatile
no cv-qualifier < const volatile
const           < const volatile
volatile        < const volatile

Although the definition is pretty clear and easy to understand, I wonder why it exists. I could not find another passage in the standard that makes use of this ordering (I might have missed something!). Cppreference has the same table, but also does not explain its use.

Why does this partial ordering exist?


Solution

  • Just search for "more cv-qualified" in the standard document. There will be a lot of matches. Maybe the most obvious example:

    char* pointer1;
    ...
    const char* pointer2 = pointer1; // OK because it's more cv-qualified
    

    In my opinion, if you are looking for common sense, just think about "const" instead of "cv".