MISRA C++ 2008, Rule 14-5-1 states that "A non-member generic function shall only be declared in a namespace that is not an associated namespace".
Considering the case of (e.g) overloading the operator<<()
, I wonder if this is not the very case to avoid with the MISRA rule. Example:
#include <iostream>
std::ostream& operator<<(std::ostream &s, int x){
s << "my operator<<" << std::endl;
return s;
}
int main() {
std::cout << 5L << std::endl;
return 0;
}
This results in not calling my output operator, but rather the STL output operator. So: is this an example of MISRA rule 14-5-1 violation where ADL picks the unintended function?
As I understand, that rule is to avoid that template (free) functions are used with ADL.
operator<<()
doesn't apply, except maybe the following (because ofT
)but anyway,
operator
are mostly useless without ADL, or else we would have to import them before there usage (as we should do for UDL asoperator ""s
)Better example would be
std::begin
which broke the rule:I don't understand which errors that rule try to prevent.