Main Content

CERT C++: DCL52-CPP

Never qualify a reference type with const or volatile

Description

Rule Definition

Never qualify a reference type with const or volatile.1

Polyspace Implementation

The rule checker checks for these issues:

  • C++ reference type qualified with const or volatile.

  • C++ reference to const-qualified type with subsequent modification.

Examples

expand all

Issue

const-Qualified Reference Type 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;
but instead declared a const-qualified reference:
char &const c;
If your compiler does not detect the error, you can see unexpected results. For instance, you might expect 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;
instead of:
char const &c;
If the qualifier is incorrectly placed, fix the error. Place the const or volatilequalifier before the & operator. Otherwise, remove the redundant qualifier.

Example – const-Qualified Reference Type
int func (int &const iRef) { //Noncompliant
    iRef++;
    return iRef%2;
}

In this example, iRef is a const-qualified reference type. Since iRef cannot refer to another variable, the const qualifier is redundant.

Correction — Remove const Qualifier

Remove the redundant const qualifier. Since iRef is modified in func, it is not meant to refer to a const-qualified variable. Moving the const qualifier before & will cause a compilation error.

int func (int &iRef) {
    iRef++;
    return iRef%2;
}
Correction — Fix Placement of const Qualifier

If you do not identify to modify iRef in func, declare iRef as a reference to a const-qualified variable. Place the const qualifier before the & operator. Make sure you do not modify iRef in func.

int func (int const &iRef) {
    return (iRef+1)%2;
}
Issue

This defect occurs when a variable that refers to a const-qualified type is modified after declaration.

For instance, in this example, refVal has a type const int &, but its value is modified in a subsequent statement.

using constIntRefType = const int &;
void func(constIntRefType refVal, int val){
   ...
   refVal = val; //refVal is modified
   ...
}

Risk

The const qualifier on a reference type implies that a variable of the type is initialized at declaration and will not be subsequently modified.

Compilers can detect modification of references to const-qualified types as a compilation error. If the compiler does not detect the error, the behavior is undefined. Polyspace® flags this defect regardless of a compilation error.

Fix

Avoid modification of const-qualified reference types. If the modification is required, remove the const qualifier from the reference type declaration.

Example – Modification of const-qualified Reference Types
typedef const int cint;           
typedef cint& ref_to_cint;       

void func(ref_to_cint refVal, int initVal){ //Noncompliant
   refVal = initVal;
}

In this example, ref_to_cint is a reference to a const-qualified type. The variable refVal of type ref_to_cint is supposed to be initialized when func is called and not modified subsequently. The modification violates the contract implied by the const qualifier. Because refVal is a const reference, the compilation might fail. Polyspace flags the violation.

Correction — Avoid Modification of const-qualified Reference Types

One possible correction is to avoid the const in the declaration of the reference type.

typedef int& ref_to_int;       

void func(ref_to_int refVal, int initVal){
   refVal = initVal;
}

Check Information

Group: 01. Declarations and Initialization (DCL)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.