Main Content

AUTOSAR C++14 Rule A13-1-2

User defined suffixes of the user defined literal operators shall start with underscore followed by one or more letters

Since R2020a

Description

Rule Definition

User defined suffixes of the user defined literal operators shall start with underscore followed by one or more letters.

Rationale

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 the Standard Library.

Polyspace Implementation

The rule checker flags definitions 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).

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Check Information

Group: Overloading
Category: Required, Automated

Version History

Introduced in R2020a