C++ reference type qualified with const or volatile
Reference type declared with a redundant const or
volatile qualifier
Description
This defect
occurs when a variable with reference type is declared with the const or
volatile qualifier, for
instance:
char &const c;
Risk
The C++14 Standard states that const or volatile
qualified references are ill formed (unless they are introduced through a
typedef, in which case they are ignored). For instance, a reference to
one variable cannot be made to refer to another variable. Therefore, using the
const qualifier is not required for a variable with a reference
type.
Often the use of these qualifiers indicate a coding error. For instance, you meant to
declare a reference to a const-qualified
type:
char const &c;
const-qualified
reference:char &const c;
c to be immutable but see a different value of c
compared to its value at declaration.Fix
See if the const or volatile qualifier is
incorrectly placed. For instance, see if you wanted to refer to a
const-qualified type and
entered:
char &const c;
char const &c;
const or
volatilequalifier before the & operator.
Otherwise, remove the redundant qualifier.Examples
Result Information
| Group: Good practice |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
CV_QUALIFIED_REFERENCE_TYPE |
| Impact: Low |
Version History
Introduced in R2019a
See Also
Qualifier removed in conversion | Unreliable cast of pointer | Unreliable cast of function pointer | C++ reference to
const-qualified type with subsequent modification | Writing to const qualified object | Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)