Main Content

AUTOSAR C++14 Rule A7-1-9

A class, structure, or enumeration shall not be declared in the definition of its type

Description

Rule Definition

A class, structure, or enumeration shall not be declared in the definition of its type.

Rationale

This rule forbids declarations of objects in the same statement as the definition of its type. For instance, the following statement defines a structure type Record while also defining an object of that type:

struct Record {
    std::uint32_t* elems;
    std::uint8_t size;
} someRecord;
Defining an entity in the same statement as the definition of its type can cause confusion to developers and reviewers.

Polyspace Implementation

The rule checker reports a violation on objects that are defined in the same statement as their type definitions.

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

#include <cstdint>
struct Record
{
    std::uint32_t* elems;
    std::uint8_t size;
} someRecord; //Noncompliant

typedef struct Item
{
    std::uint32_t* elems;
    std::uint8_t size;
} Item;

Item someItem; //Compliant

This example shows a noncompliant and compliant object definition:

  • The object someRecord is defined in the same statement as the definition of its type struct Record. Therefore, the object definition violates the rule.

  • The object someItem is defined in a separate statement from the definition of its type struct Item. Therefore, the object definition does not violate the rule.

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019a