Main Content

CERT C++: DCL51-CPP

Do not declare or define a reserved identifier

Description

Rule Definition

Do not declare or define a reserved identifier.1

Polyspace Implementation

This checker checks for:

  • Defining or undefining reserved identifier or macro

  • User-defined literal operator not starting with underscore

Examples

expand all

Issue

The issue occurs when you define, redefine, or undefine a reserved identifier, macro, or function in the Standard Library.

In general, the checker considers identifiers and macros that begin with an underscore followed by an uppercase letter as reserved for the Standard Library.

Issue

This issue occurs when you define operators of the form

operator "" suffix
where suffix does not begin with an underscore or following the underscore, contains characters other than letters (numbers, special characters, and so on).

Risk

Since C++11, you can add suffixes to literals that convert numeric values under the hood. For instance, in code where you perform all calculations in a common unit, you can leave unit conversions to dedicated operators and simply use literal suffixes for the units when defining constant values.

In this example, the literal suffixes _m and _km resolve to calls to operator"" _m() and operator"" _km() respectively. The operators ensure that all values are converted to the same unit.

constexpr long double operator"" _m(long double metres) {
    return metres;
}

constexpr long double operator"" _km(long double kilometres) {
    return 1000*kilometres;
}
...
long double minSteps = 100.0_m;
long double interCityDist = 100.0_km;

User defined literal suffixes must begin with an underscore (_). Literal suffixes not beginning with underscore are reserved for Standard Library.

Fix

Make sure that user-defined literal operators begin with an underscore followed by letters only.

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.