AutosarC++17_10-A11.0.2

A type defined as struct shall: (1) provide only public data members, (2) not provide any special member functions or methods, (3) not be a base of another struct or class, (4) not inherit from another struct or class

Required inputs: IR

Rationale

This is consistent with developer expectations that a class provides its invariant, interface and encapsulation guarantee, while a struct is only an aggregate without any class-like features.

An example of a struct type is POD type.

See: POD-type.

Example
// $Id: A11-0-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
struct A // Compliant
{
  std::int32_t x;
  double y;
};
struct B // Compliant
{
  std::uint8_t x;
  A a;
};
struct C // Compliant
{
  float x = 0.0f;
  std::int32_t y = 0;
  std::uint8_t z = 0U;
};
struct D // Non-compliant
{
public:
  std::int32_t x;

protected:
  std::int32_t y;

private:
  std::int32_t z;
};
struct E // Non-compliant
{
public:
  std::int32_t x;
  void Fn() noexcept {}

private:
  void F1() noexcept(false) {}
};
struct F : public D // Non-compliant
{};
See Also
  • stackoverflow.com [16]: When should you use a class vs a struct in C++?

Possible Messages

Key

Text

Severity

Disabled

no_function_member

Struct shall not provide any member functions or methods.

None

False

no_struct_as_base

A struct shall not be a base of another struct or class.

None

False

no_struct_inheritance

A struct shall not inherit from another struct or class.

None

False

non_public_member

Structs shall only have public data members.

None

False

Options

ignore_template_specializations

ignore_template_specializations : bool = False

Whether special member functions or methods in template specializations should be ignored and not considered a violation.