CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Clarification on the scope of rule C.83

Open doxygen opened this issue 5 years ago • 2 comments

The company I work for employs a third party static code quality checker. This tool has recently adopted a number of additional checks. One of them is rule C.83. The tool very literally implements the mentioned enforcement "A class without virtual functions should have a swap member function declared."

The consequence of this is that any Plain Old Datatype and non-polymorphic class is flagged if it misses the swap method. The solution advised is to add

class C
{
   ...
   void swap(C &other) noexcept { std::swap(*this, other); } 
   ...
};

in such cases.

I wonder if this was the intention of the rule and if perhaps some context is missing as to when to apply this rule.

doxygen avatar Sep 26 '20 15:09 doxygen

std::swap(*this, other);

That's absurd. If a member swap can't be any more efficient than std::swap then I would consider adding one, and decide not to.

If it can be more efficient, then that suggested solution is bad advice.

I agree the enforcement seems too broad without exceptions to say when it's ok to ignore.

jwakely avatar Sep 26 '20 16:09 jwakely

The right question should be, what is the definition of value-like type C.83 is based on?

iglesias avatar Oct 02 '20 07:10 iglesias