Hello,
I use SonarQube for Visual Studio 9.9.0.16495 for C++17 code, and get the S2807 issue for this code (on operator<<):
struct LogContext
{
const char* file;
int32_t line;
const char* function;
std::string message;
};
inline std::ostream& operator<<( std::ostream& os, const LogContext& context )
{
os << context.file << " - " << context.line << " - " << context.function;
if( !context.message.empty() )
{
os << " - " << context.message;
}
return os;
}
As the fields are public, there is no need to declare the free function operator<< as friend of LogContext.
The rule C.161 says to use free operators instead of memebr operators, it doesn’t say to add the operator as friend. It may be the logical consequence. Or not.