Main Content

MISRA C++:2008 Rule 16-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

Version History

Introduced in R2013b