QPair: add constexpr liberally
On templates, adding constexpr makes a given instantiation constexpr if it can be. This turns qMakePair(0,0), say, into a compile-time constant. The effects on existing code are small, but exist: $ size lib/*{-baseline,-paircexp} | sort -nr 6516727 211192 2608 6730527 66b31f lib/libQt5Widgets.so.5.4.0-baseline 6516711 211192 2608 6730511 66b30f lib/libQt5Widgets.so.5.4.0-paircexp 5373720 44492 15976 5434188 52eb4c lib/libQt5Core.so.5.4.0-baseline 5373504 44492 15976 5433972 52ea74 lib/libQt5Core.so.5.4.0-paircexp 5107206 125072 6080 5238358 4fee56 lib/libQt5Gui.so.5.4.0-baseline 5107030 125072 6080 5238182 4feda6 lib/libQt5Gui.so.5.4.0-paircexp 1341290 30180 2600 1374070 14f776 lib/libQt5Network.so.5.4.0-baseline 1341210 30180 2600 1373990 14f726 lib/libQt5Network.so.5.4.0-paircexp # no other libraries benefit [ChangeLog][QtCore][QPair] Can now be used in C++11 constexpr contexts. Change-Id: I3872e6aa33a7d02a168516f4dfa7119efcac8c40 Reviewed-by: Olivier Goffart <ogoffart@woboq.com> Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
parent
31f2b7e591
commit
e41b45f1ae
@ -53,18 +53,18 @@ struct QPair
|
|||||||
typedef T1 first_type;
|
typedef T1 first_type;
|
||||||
typedef T2 second_type;
|
typedef T2 second_type;
|
||||||
|
|
||||||
QPair() : first(), second() {}
|
Q_DECL_CONSTEXPR QPair() : first(), second() {}
|
||||||
QPair(const T1 &t1, const T2 &t2) : first(t1), second(t2) {}
|
Q_DECL_CONSTEXPR QPair(const T1 &t1, const T2 &t2) : first(t1), second(t2) {}
|
||||||
// compiler-generated copy/move ctor/assignment operators are fine!
|
// compiler-generated copy/move ctor/assignment operators are fine!
|
||||||
|
|
||||||
template <typename TT1, typename TT2>
|
template <typename TT1, typename TT2>
|
||||||
QPair(const QPair<TT1, TT2> &p) : first(p.first), second(p.second) {}
|
Q_DECL_CONSTEXPR QPair(const QPair<TT1, TT2> &p) : first(p.first), second(p.second) {}
|
||||||
template <typename TT1, typename TT2>
|
template <typename TT1, typename TT2>
|
||||||
QPair &operator=(const QPair<TT1, TT2> &p)
|
QPair &operator=(const QPair<TT1, TT2> &p)
|
||||||
{ first = p.first; second = p.second; return *this; }
|
{ first = p.first; second = p.second; return *this; }
|
||||||
#ifdef Q_COMPILER_RVALUE_REFS
|
#ifdef Q_COMPILER_RVALUE_REFS
|
||||||
template <typename TT1, typename TT2>
|
template <typename TT1, typename TT2>
|
||||||
QPair(QPair<TT1, TT2> &&p) : first(std::move(p.first)), second(std::move(p.second)) {}
|
Q_DECL_CONSTEXPR QPair(QPair<TT1, TT2> &&p) : first(std::move(p.first)), second(std::move(p.second)) {}
|
||||||
template <typename TT1, typename TT2>
|
template <typename TT1, typename TT2>
|
||||||
QPair &operator=(QPair<TT1, TT2> &&p)
|
QPair &operator=(QPair<TT1, TT2> &&p)
|
||||||
{ first = std::move(p.first); second = std::move(p.second); return *this; }
|
{ first = std::move(p.first); second = std::move(p.second); return *this; }
|
||||||
@ -80,39 +80,39 @@ template<class T1, class T2>
|
|||||||
class QTypeInfo<QPair<T1, T2> > : public QTypeInfoMerger<QPair<T1, T2>, T1, T2> {}; // Q_DECLARE_TYPEINFO
|
class QTypeInfo<QPair<T1, T2> > : public QTypeInfoMerger<QPair<T1, T2>, T1, T2> {}; // Q_DECLARE_TYPEINFO
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator==(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator==(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{ return p1.first == p2.first && p1.second == p2.second; }
|
{ return p1.first == p2.first && p1.second == p2.second; }
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator!=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator!=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{ return !(p1 == p2); }
|
{ return !(p1 == p2); }
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator<(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator<(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{
|
{
|
||||||
return p1.first < p2.first || (!(p2.first < p1.first) && p1.second < p2.second);
|
return p1.first < p2.first || (!(p2.first < p1.first) && p1.second < p2.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator>(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator>(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{
|
{
|
||||||
return p2 < p1;
|
return p2 < p1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator<=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator<=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{
|
{
|
||||||
return !(p2 < p1);
|
return !(p2 < p1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_INLINE_TEMPLATE bool operator>=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
Q_DECL_CONSTEXPR Q_INLINE_TEMPLATE bool operator>=(const QPair<T1, T2> &p1, const QPair<T1, T2> &p2)
|
||||||
{
|
{
|
||||||
return !(p1 < p2);
|
return !(p1 < p2);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T1, class T2>
|
template <class T1, class T2>
|
||||||
Q_OUTOFLINE_TEMPLATE QPair<T1, T2> qMakePair(const T1 &x, const T2 &y)
|
Q_DECL_CONSTEXPR Q_OUTOFLINE_TEMPLATE QPair<T1, T2> qMakePair(const T1 &x, const T2 &y)
|
||||||
{
|
{
|
||||||
return QPair<T1, T2>(x, y);
|
return QPair<T1, T2>(x, y);
|
||||||
}
|
}
|
||||||
|
@ -42,12 +42,13 @@
|
|||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
|
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
|
#include <QSize>
|
||||||
|
|
||||||
class tst_QPair : public QObject
|
class tst_QPair : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void dummy() {}
|
void testConstexpr();
|
||||||
};
|
};
|
||||||
|
|
||||||
class C { char _[4]; };
|
class C { char _[4]; };
|
||||||
@ -101,5 +102,19 @@ Q_STATIC_ASSERT(!QTypeInfo<QPairPP>::isDummy );
|
|||||||
Q_STATIC_ASSERT(!QTypeInfo<QPairPP>::isPointer);
|
Q_STATIC_ASSERT(!QTypeInfo<QPairPP>::isPointer);
|
||||||
|
|
||||||
|
|
||||||
|
void tst_QPair::testConstexpr()
|
||||||
|
{
|
||||||
|
Q_CONSTEXPR QPair<int, double> pID = qMakePair(0, 0.0);
|
||||||
|
Q_UNUSED(pID);
|
||||||
|
|
||||||
|
Q_CONSTEXPR QPair<double, double> pDD = qMakePair(0.0, 0.0);
|
||||||
|
Q_CONSTEXPR QPair<double, double> pDD2 = qMakePair(0, 0.0); // involes (rvalue) conversion ctor
|
||||||
|
Q_CONSTEXPR bool equal = pDD2 == pDD;
|
||||||
|
QVERIFY(equal);
|
||||||
|
|
||||||
|
Q_CONSTEXPR QPair<QSize, int> pSI = qMakePair(QSize(4, 5), 6);
|
||||||
|
Q_UNUSED(pSI);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(tst_QPair)
|
QTEST_APPLESS_MAIN(tst_QPair)
|
||||||
#include "tst_qpair.moc"
|
#include "tst_qpair.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user