QFlags: make base class SMFs protected

This is from one of the 90s books on C++ Coding Style:

  Destructors of (public) base classes should be either public and
  virtual or else protected and non-virtual.

The dtors of the various QFlags helper classes were public and
non-virtual, allowing slicing and use outside of QFlags.

Fix both following the rule and making the dtors protected.

Previously, this would have been enough, but Clang started to warn
about undeclared copy- and move-SMFs in the presence of a user-declared
dtor, so need to declare all five.

Found in API-review.

Pick-to: 6.9
Change-Id: I70d1163f7bb42d981bf550418d2775a784bd416e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Marc Mutz 2025-01-29 14:45:40 +01:00
parent 1a74f77f57
commit f080f16123

View File

@ -71,6 +71,7 @@ protected:
Int i = 0;
QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorage)
public:
constexpr inline QFlagsStorage() noexcept = default;
constexpr inline explicit QFlagsStorage(std::in_place_t, Int v) : i(v) {}
@ -80,6 +81,8 @@ template <typename Enum, int Size = sizeof(QFlagsStorage<Enum>)>
struct QFlagsStorageHelper : QFlagsStorage<Enum>
{
using QFlagsStorage<Enum>::QFlagsStorage;
protected:
QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorageHelper)
};
template <typename Enum> struct QFlagsStorageHelper<Enum, sizeof(int)> : QFlagsStorage<Enum>
{
@ -94,6 +97,8 @@ template <typename Enum> struct QFlagsStorageHelper<Enum, sizeof(int)> : QFlagsS
#ifdef QT_TYPESAFE_FLAGS
constexpr inline explicit operator QFlag() const noexcept { return QFlag(this->i); }
#endif
protected:
QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorageHelper)
};
} // namespace QtPrivate