Main Content

AUTOSAR C++14 Rule M16-3-1

There shall be at most one occurrence of the # or ## operators in a single macro definition

Description

Rule Definition

There shall be at most one occurrence of the # or ## operators in a single macro definition.

Rationale

The evaluation of the # and ## preprocessor operators does not have a specified execution order. When more than one occurrence of the # or ## operators exists in a single macro definition, it is unclear which preprocessor operator is executed first by the compiler. The uncertainty of execution order might result in developer confusion or unexpected macro calculations. Use only one of the # or ## preprocessor operators for each macro definition.

Polyspace Implementation

Polyspace ®raises this defect whenever more than one instance of the # or ## operators is used in a single macro definition.

Troubleshooting

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

Examples

expand all

#define STRING(X) { #X }                          //Compliant
#define CONCAT(X, Y) {X ## Y}                     //Compliant
#define STRING_CONCAT(x, y) {#x ## y}             //Noncompliant
#define MULTI_CONCAT(x, y, xy, z) {x ## y ## z}   //Noncompliant

Because the macro STRING_CONCAT uses both the # and ## operators, Polyspace flags the macro as noncompliant.

Polyspace flags the macro MULTI_CONCAT as noncompliant because it uses multiple ## operators.

Check Information

Group: Preprocessing Directives
Category: Required, Automated

Version History

Introduced in R2019a