From 47eaa2b1a58db67643f32cb1d8779c5b199ccb10 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Wed, 4 Sep 2019 21:29:55 +0200 Subject: QCanBusDevice: Make Filter comparison functions non-member friends MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Alex Blasche Reviewed-by: Mårten Nordheim --- src/serialbus/qcanbusdevice.cpp | 10 ++++++---- src/serialbus/qcanbusdevice.h | 10 +++++----- 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 { -- cgit v1.2.3