rspec/rules/S3692/cfamily/rule.adoc

38 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
Making a comparison operator ``++virtual++`` implies that you want to compare objects of different types by overriding ``++operator==++``, for instance, in a subclass to compare instances of the base class with instances of the subclass. But polymorphic comparison operators are very difficult to get right, and are actually questionable in concept. After all, can two objects with only a few common members really be equal?
This rule raises issues on ``++virtual++`` comparison operators.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
struct Foo {
virtual bool operator==(const Foo &other) const; // Noncompliant
virtual bool operator!=(const Foo &other) const; // Noncompliant
};
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
struct Foo {
bool operator==(const Foo &other) const;
bool operator!=(const Foo &other) const;
};
----
2021-04-28 16:49:39 +02:00
== See
* https://github.com/isocpp/CppCoreGuidelines/blob/036324/CppCoreGuidelines.md#c87-beware-of\--on-base-classes[{cpp} Core Guidelines C.87] - Beware of ++==++ on base classes
ifdef::rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::rspecator-view[]