07-11-2023, 02:12 PM
(This post was last modified: 08-11-2023, 07:16 AM by Jakob Klein.)
Hi
A4-5-1 states:
My use-case is overloading operator<< for convenient logging and printing of an unscoped enum:
Usage, where the warning is shown:
So as long as the enum is not used in arithmetic context ie. bitwise operations (the enum is the left-hand operand) but with the streeam insertion operator I can not follow the rationale of the rule. Is this an oversight in the rule or is there a more specific reason behind this?
A4-5-1 states:
Quote: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 <, <=, >, >=.My understanding of the rule is that confusion should be avoided when using the underlying type arithmetically.
My use-case is overloading operator<< for convenient logging and printing of an unscoped enum:
Code:
inline std::ostream& operator<<(std::ostream& os, MyEnum const& enum_value)
{
switch (enum_value)
{
case MyEnum_Unknown:
{
return os << "Unknown";
}
case MyEnum_Val1:
{
return os << "Descriptive message... (MyEnum_Val1)";
}//...
}
}
Usage, where the warning is shown:
Code:
LOG_WARNING << "Could not perform action with " << enum_value;
So as long as the enum is not used in arithmetic context ie. bitwise operations (the enum is the left-hand operand) but with the streeam insertion operator I can not follow the rationale of the rule. Is this an oversight in the rule or is there a more specific reason behind this?