AutosarC++19_03-A7.2.2¶
Enumeration underlying base type shall be explicitly defined
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
The enumeration underlying type is implementation-defined, with the only restriction that the type must be able to represent the enumeration values. Although scoped enum will implicitly define an underlying type of int, the underlying base type of enumeration should always be explicitly defined with a type that will be large enough to store all enumerators.
Example
// $Id: A7-2-2.cpp 271715 2017-03-23 10:13:51Z piotr.tanski $
#include <cstdint>
enum class E1 // Non-compliant
{
E10,
E11,
E12
};
enum class E2 : std::uint8_t // Compliant
{
E20,
E21,
E22
};
enum E3 // Non-compliant
{
E30,
E31,
E32
};
enum E4 : std::uint8_t // Compliant - violating another rule
{
E40,
E41,
E42
};
enum class E5 : std::uint8_t // Non-compliant - will not compile
{
E50 = 255,
// E5_1, // E5_1 = 256 which is outside of range of underlying type
// std::uint8_t
// - compilation error
// E5_2 // E5_2 = 257 which is outside of range of underlying type
// std::uint8_t
// - compilation error
};
See Also
- HIC++ v4.0 [9]: 7.2.1 Use an explicit enumeration base and ensure that it is large enough to store all enumerators
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
unbased_enum |
Enumeration underlying base type shall be explicitly defined. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
allow_enumerations_inside_extern_c¶
allow_enumerations_inside_extern_c : bool = False
allow_enumerations_only_using_defaults¶
allow_enumerations_only_using_defaults : bool = False