AUTOSAR C++14 Rule A14-5-3
A non-member generic operator shall only be declared in a namespace that does not contain class (struct) type, enum type or union type declarations
Description
Rule Definition
A non-member generic operator shall only be declared in a namespace that does not contain class (struct) type, enum type or union type declarations.
Rationale
This rule forbids placing generic operators in the same namespace as class (struct) type, enum type, or union type declarations. If the class, enum or union types are used as template parameters, the presence of generic operators in the same namespace can cause unexpected call resolutions.
Consider the namespace NS
that combines a class B
and a generic form of
operator==
:
namespace NS { class B {}; template <typename T> bool operator==(T, std::int32_t); }
B
as a template parameter for another generic class, such
as this template class
A
:template <typename T> class A { public: bool operator==(std::int64_t); } template class A<NS::B>;
NS
is used for overload resolution when operators of
class A
are called. For instance, if you call
operator==
with an int32_t
argument, the generic
operator==
in the namespace NS
with an
int32_t
parameter is used instead of the operator==
in the original template class A
with an int64_t
parameter. You or another developer or code reviewer might expect the operator call to
resolve to the operator==
in the original template class
A
.Polyspace Implementation
For each generic operator, the rule checker determines if the containing namespace also contains declarations of class types, enum types, or union types. If such a declaration is found, the checker flags a rule violation on the operator itself.
The checker also flags generic operators defined in the global namespace if the global namespace also has class, enum or union declarations.
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
Check Information
Group: Templates |
Category: Advisory, Automated |