rspec/rules/S3692/cfamily/rule.adoc

60 lines
1.5 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
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.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
struct Foo {
virtual bool operator==(const Foo &other) const; // Noncompliant
virtual bool operator!=(const Foo &other) const; // Noncompliant
};
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,cpp]
2021-04-28 16:49:39 +02:00
----
struct Foo {
bool operator==(const Foo &other) const;
bool operator!=(const Foo &other) const;
};
----
== Resources
2021-04-28 16:49:39 +02:00
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#c87-beware-of\--on-base-classes[C.87: Beware of `==` on base classes]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this "virtual" specifier and refactor the code to not require polymorphism for comparison operators.
=== Highlighting
virtual keyword
'''
== Comments And Links
(visible only on this page)
=== on 5 Aug 2016, 14:42:13 Ann Campbell wrote:
See what you think [~alban.auzeill]
endif::env-github,rspecator-view[]