Versions Compared

Key

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

...

Code Block
error: 'const' qualifier may not be applied to a reference

...

Noncompliant Code Example

This compliant solution assumes the programmer intended for the previous noncompliant code example to fail to compile due to attempting to modify a correctly declares p to be a reference to a const-qualified char reference:. The subsequent modification of p makes the program ill-formed.

Code Block
bgColor#ccccff#ffcccc
langcpp
#include <iostream>
  
void f(char c) {
  const char &p = c;
  p = 'p'; // Error: read-only variable is not assignable
  std::cout << c << std::endl;
}

Compliant Solution

This compliant solution removes the const qualifier.

Code Block
bgColor#ccccff
langcpp
#include <iostream>
 
void f(char c) {
  char &p = c;
  p = 'p';
  std::cout << c << std::endl;
}

...