AutosarC++17_03-A4.5.1

Expressions with type enum or enum class shall not be used as operands to built-in and overloaded operators other than the subscript operator [ ], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=

Required inputs: IR

Rationale

Enumerations, i.e. enums or enum classes, have implementation-defined representations and they are not supposed to be used in arithmetic contexts.

Note that only enums can be implicitly used as operands to other built-in operators, like operators +, −, ∗, etc. Enum class needs to provide definitions of mentioned operators in order to be used as operand.

Exceptions

It is allowed to use the enumeration as operand to all built-in and overloaded operators if the enumeration satisfies the “BitmaskType” concept [15].

Example
// $Id: A4-5-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
#include <cstdint>
enum Colour : std::uint8_t { Red, Green, Blue, ColoursCount };
void f1() noexcept(false) {
Colour colour = Red;
if (colour == Green) // Compliant
{
}

if (colour == (Red + Blue)) // Non-compliant
{
}

if (colour < ColoursCount) // Compliant
{
}
}
enum class Car : std::uint8_t { Model1, Model2, Model3, ModelsCount };
void f2() noexcept(false) {
Car car = Car::Model1;
if (car != Car::Model2) // Compliant
{
}

if (car == Car::Model3) // Compliant
{
}

// if (car == (Car::Model1 + Car::Model2)) // Non-compliant -
// operator+ not provided for Car enum class, compilation error
//{
//}
if (car < Car::ModelsCount) // Compliant
{
}
}
Car operator+(Car lhs, Car rhs) { return Car::Model3; }
void f3() noexcept(false) {
Car car = Car::Model3;
if (car == (Car::Model1 + Car::Model2)) // Non-compliant - overloaded
                                  // operator+ provided, no
                                  // compilation error
{
}
}
enum Team : std::uint8_t {
TeamMember1 = 0,
TeamMember2 = 1,
TeamMember3 = 2,
TeamMember4 = 3,
TeamMembersStart = TeamMember1,
TeamMembersEnd = TeamMember2,
TeamMembersCount = 4
};
void f4(const char *teamMember) {
// Implementation
}
void f5() {
const char *team[TeamMembersCount]; // Compliant
// ...
f4(team[TeamMember2]); // Compliant
}
See Also
  • MISRA C++ 2008 [6]: Rule 4-5-2 Expressions with type enum shall not be used as operands to built-in operators other than the subscript operator [ ], the assignment operator =, the equality operators == and !=, the unary & operator, and the relational operators <, <=, >, >=.

Possible Messages

Key

Text

Severity

Disabled

enum_operand_outside_comparison

Use of enum operand in arithmetic or similar context

None

False

Options

allow_bitmask_type

allow_bitmask_type : bool = True

If set to true, allow enum/enum class as operands to arithmetic operators if they satisfy the "BitmaskType" concept. Note: The Axivion Suite does not detect if a class actually satisfy the "BitmaskType" concept, please refer to the option fully_qualified_bitmask_types.
 

fully_qualified_bitmask_types

fully_qualified_bitmask_types

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'std::_Codecvt_mode', 'std::_Iosb::_Fmtflags', 'std::_Iosb::_Iostate', 'std::_Iosb::_Openmode', 'std::ctype_base::mask', 'std::filesystem::copy_options', 'std::filesystem::directory_options', 'std::filesystem::perms', 'std::ios_base::fmtflags', 'std::ios_base::iostate', 'std::ios_base::openmode', 'std::launch', 'std::regex_constants::match_flag_type', 'std::regex_constants::syntax_option_type', 'std::regex_traits::char_class_type'}

A set of fully qualified types that satisfy the "BitmaskType" concept. This has no effect if allow_bitmask_type is not enabled.
 

report_use_in_operator_calls

report_use_in_operator_calls : bool = True

Whether enum arguments in calls to forbidden overloaded operators should be reported.