Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When attempting to const-qualify a value of reference type, a programmer may accidentally write:

Code Block
bgColor#ffcccc
langcpp
char &const p;

instead of:

Code Block
bgColor#ccccff
langcpp
char const &p; // or: const char &p;

Do not attempt to cv-qualify a reference type as because it can result in undefined behavior. A conforming compiler is required to issue a diagnostic message. However, if the compiler does not emit a fatal diagnostic, the program may produce surprising results, such as allowing the character referenced by p to be mutated.

...

In this noncompliant code example, a const-qualified reference to a char is formed instead of a reference to a const-qualified char is formed, resulting in undefined behavior:

...

With Microsoft Visual Studio 2013, this code compiles successfully with a warning diagnostic (warning C4227: anachronism used : qualifiers on reference are ignored) and outputs:

Code Block
p

With Clang 3.5, this code produces a fatal diagnostic:

...

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...