diff options
6 files changed, 74 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index bbcb1d5880..08e631568d 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -617,6 +617,24 @@ Q_DECL_CONSTEXPR inline qint64 qRound64(double d) Q_DECL_CONSTEXPR inline qint64 qRound64(float d) { return d >= 0.0f ? qint64(d + 0.5f) : qint64(d - float(qint64(d-1)) + 0.5f) + qint64(d-1); } +namespace QTypeTraits { + +namespace detail { +template<typename T, typename U, + typename = std::enable_if_t<std::is_arithmetic_v<T> && std::is_arithmetic_v<U> && + std::is_floating_point_v<T> == std::is_floating_point_v<U> && + std::is_signed_v<T> == std::is_signed_v<U>> > +struct Promoted +{ + using type = decltype(T() + U()); +}; +} + +template <typename T, typename U> +using Promoted = typename detail::Promoted<T, U>::type; + +} + template <typename T> constexpr inline const T &qMin(const T &a, const T &b) { return (a < b) ? a : b; } template <typename T> @@ -624,6 +642,31 @@ constexpr inline const T &qMax(const T &a, const T &b) { return (a < b) ? b : a; template <typename T> constexpr inline const T &qBound(const T &min, const T &val, const T &max) { return qMax(min, qMin(max, val)); } +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qMin(const T &a, const U &b) +{ + using P = QTypeTraits::Promoted<T, U>; + P _a = a; + P _b = b; + return (_a < _b) ? _a : _b; +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qMax(const T &a, const U &b) +{ + using P = QTypeTraits::Promoted<T, U>; + P _a = a; + P _b = b; + return (_a < _b) ? _b : _a; +} +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const U &val, const T &max) +{ return qMax(min, qMin(max, val)); } +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const T &min, const T &val, const U &max) +{ return qMax(min, qMin(max, val)); } +template <typename T, typename U> +constexpr inline QTypeTraits::Promoted<T, U> qBound(const U &min, const T &val, const T &max) +{ return qMax(min, qMin(max, val)); } #ifndef Q_FORWARD_DECLARE_OBJC_CLASS # ifdef __OBJC__ diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp index 7787c00218..4cfbdbde37 100644 --- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp +++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp @@ -51,6 +51,7 @@ private slots: void integerForSize(); void buildAbiEndianness(); void testqOverload(); + void testqMinMax(); }; extern "C" { // functions in qglobal.c @@ -575,6 +576,32 @@ void tst_QGlobal::testqOverload() #endif } +// enforce that types are identical when comparing +template<typename T> +void compare(T a, T b) +{ QCOMPARE(a, b); } + +void tst_QGlobal::testqMinMax() +{ + // signed types + compare(qMin(float(1), double(-1)), double(-1)); + compare(qMin(double(1), float(-1)), double(-1)); + compare(qMin(short(1), int(-1)), int(-1)); + compare(qMin(short(1), long(-1)), long(-1)); + compare(qMin(qint64(1), short(-1)), qint64(-1)); + + compare(qMax(float(1), double(-1)), double(1)); + compare(qMax(short(1), long(-1)), long(1)); + compare(qMax(qint64(1), short(-1)), qint64(1)); + + // unsigned types + compare(qMin(ushort(1), ulong(2)), ulong(1)); + compare(qMin(quint64(1), ushort(2)), quint64(1)); + + compare(qMax(ushort(1), ulong(2)), ulong(2)); + compare(qMax(quint64(1), ushort(2)), quint64(2)); +} + QTEST_APPLESS_MAIN(tst_QGlobal) #include "tst_qglobal.moc" diff --git a/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp b/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp index fcf004b4fc..1437717201 100644 --- a/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp +++ b/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/tst_qstringbuilder1.cpp @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include <QtCore/qglobal.h> // SCENARIO 1 // this is the "no harm done" version. Only operator% is active, diff --git a/tests/auto/corelib/text/qstringbuilder/qstringbuilder2/tst_qstringbuilder2.cpp b/tests/auto/corelib/text/qstringbuilder/qstringbuilder2/tst_qstringbuilder2.cpp index ac4e383649..8b6350a65d 100644 --- a/tests/auto/corelib/text/qstringbuilder/qstringbuilder2/tst_qstringbuilder2.cpp +++ b/tests/auto/corelib/text/qstringbuilder/qstringbuilder2/tst_qstringbuilder2.cpp @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include <QtCore/qglobal.h> // SCENARIO 2 // this is the "full" version. Operator+ is replaced by a QStringBuilder diff --git a/tests/auto/corelib/text/qstringbuilder/qstringbuilder3/tst_qstringbuilder3.cpp b/tests/auto/corelib/text/qstringbuilder/qstringbuilder3/tst_qstringbuilder3.cpp index 6559168cbd..6c5994f7b3 100644 --- a/tests/auto/corelib/text/qstringbuilder/qstringbuilder3/tst_qstringbuilder3.cpp +++ b/tests/auto/corelib/text/qstringbuilder/qstringbuilder3/tst_qstringbuilder3.cpp @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include <QtCore/qglobal.h> // SCENARIO 3 // this is the "no harm done" version. Only operator% is active, diff --git a/tests/auto/corelib/text/qstringbuilder/qstringbuilder4/tst_qstringbuilder4.cpp b/tests/auto/corelib/text/qstringbuilder/qstringbuilder4/tst_qstringbuilder4.cpp index 4efc69908e..2aecdcfff6 100644 --- a/tests/auto/corelib/text/qstringbuilder/qstringbuilder4/tst_qstringbuilder4.cpp +++ b/tests/auto/corelib/text/qstringbuilder/qstringbuilder4/tst_qstringbuilder4.cpp @@ -26,6 +26,7 @@ ** ****************************************************************************/ +#include <QtCore/qglobal.h> // SCENARIO 4 // this is the "full" version. Operator+ is replaced by a QStringBuilder |