Main Content

MISRA C++:2023 Rule 19.6.1

The #pragma directive and the _Pragma operator should not be used

Since R2024b

Description

Rule Definition

The #pragma directive and the _Pragma operator should not be used.

Rationale

The use of the #pragma directive in your code results in implementation-defined behavior. The directive might also not be supported by certain compilers.

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

//header.h
#pragma once //Noncompliant

#ifndef HEADER_H_ //Compliant
#define HEADER_H_
// ...
// body of header file
//..
#endif

The #pragma once directive prevents the inclusion of header.h more than once. However, if you copy header.h into multiple project modules, the directive may or may not treat the copies as the same file depending on the implementation. To avoid double definitions, use the #ifndef include guard instead.

Check Information

Group: Preprocessing Directives
Category: Advisory

Version History

Introduced in R2024b