summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndre Hartmann <aha_1980@gmx.de>2019-09-04 21:29:55 +0200
committerMarc Mutz <marc.mutz@kdab.com>2019-09-10 11:09:00 +0000
commit47eaa2b1a58db67643f32cb1d8779c5b199ccb10 (patch)
treedfc1897681d1e88fe7f4377d60ac2edcb3daf9b9
parent35e033232f8b19fcd3785baf04fec0db546d8015 (diff)
QCanBusDevice: Make Filter comparison functions non-member friends
Symmetric operators, such as op== and op!=, shouldn't be members, because member functions do not allow for implicit conversions on the LHS argument, which introduces an asymmetry in how the RHS and LHS arguments are handled by the compiler. We could add the operators as non-member non-friend free functions, but since Filter is a nested class, there's a lot of code between the definiton of Filter and the point where we can lexically declare the operators for the first time (after the definition of the outer class). Code between these points does not see the operators and thus behaves different from code that follows the the definition of the outer class. This is subtle and while in the present case, there's no indication that Filter may get an implicit conversion to some other type which does have an equality operator, there's always the possibility that such a thing might be added later, in which case code using lhsfilter == rhsfilter in the body of QCanBusDevice would call a different operator== than code outside it. It's just safer to declare the operators as soon as possible, which means as non-member friends. Change-Id: Iadd097a9d64d0b1c912b750d6d89431b42def313 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Alex Blasche <alexander.blasche@qt.io> Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
-rw-r--r--src/serialbus/qcanbusdevice.cpp10
-rw-r--r--src/serialbus/qcanbusdevice.h10
2 files changed, 11 insertions, 9 deletions
diff --git a/src/serialbus/qcanbusdevice.cpp b/src/serialbus/qcanbusdevice.cpp
index 8eb45c1..222f19c 100644
--- a/src/serialbus/qcanbusdevice.cpp
+++ b/src/serialbus/qcanbusdevice.cpp
@@ -150,16 +150,18 @@ Q_LOGGING_CATEGORY(QT_CANBUS, "qt.canbus")
*/
/*!
- \fn QCanBusDevice::Filter::operator==(const Filter &other)
+ \fn bool operator==(const QCanBusDevice::Filter &a, const QCanBusDevice::Filter &b)
+ \relates QCanBusDevice::Filter
- Returns true, if the filter \a other is equal to this filter,
+ Returns true, if the filter \a a is equal to the filter \a b,
otherwise returns false.
*/
/*!
- \fn QCanBusDevice::Filter::operator!=(const Filter &other)
+ \fn bool operator!=(const QCanBusDevice::Filter &a, const QCanBusDevice::Filter &b)
+ \relates QCanBusDevice::Filter
- Returns true, if the filter \a other is not equal to this filter,
+ Returns true, if the filter \a a is not equal to the filter \a b,
otherwise returns false.
*/
diff --git a/src/serialbus/qcanbusdevice.h b/src/serialbus/qcanbusdevice.h
index 9b505f9..5d2d976 100644
--- a/src/serialbus/qcanbusdevice.h
+++ b/src/serialbus/qcanbusdevice.h
@@ -98,15 +98,15 @@ public:
struct Filter
{
- bool operator==(const Filter &other) const
+ friend constexpr bool operator==(const Filter &a, const Filter &b) noexcept
{
- return frameId == other.frameId && frameIdMask == other.frameIdMask
- && type == other.type && format == other.format;
+ return a.frameId == b.frameId && a.frameIdMask == b.frameIdMask
+ && a.type == b.type && a.format == b.format;
}
- bool operator!=(const Filter &other) const
+ friend constexpr bool operator!=(const Filter &a, const Filter &b) noexcept
{
- return !operator==(other);
+ return !operator==(a, b);
}
enum FormatFilter {