core: Add deduction guides for QPair

[ChangeLog][QtCore] Added support of deduction guides for QPair

Change-Id: I41a798390dc2c925b0f8432ba12aa345724de2d7
Reviewed-by: Martin Smith <martin.smith@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
bb10
Mikhail Svetkin 2019-03-08 15:38:33 +01:00
parent 461e89ee1a
commit 4054759aec
3 changed files with 34 additions and 0 deletions

View File

@ -97,6 +97,11 @@ struct QPair
T2 second;
};
#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
template<class T1, class T2>
QPair(T1, T2) -> QPair<T1, T2>;
#endif
template <typename T1, typename T2>
void swap(QPair<T1, T2> &lhs, QPair<T1, T2> &rhs) Q_DECL_NOEXCEPT_EXPR(noexcept(lhs.swap(rhs)))
{ lhs.swap(rhs); }

View File

@ -2,3 +2,6 @@ CONFIG += testcase
TARGET = tst_qpair
QT = core testlib
SOURCES = tst_qpair.cpp
# Force C++17 if available (needed due to Q_COMPILER_DEDUCTION_GUIDES)
contains(QT_CONFIG, c++1z): CONFIG += c++1z

View File

@ -39,6 +39,7 @@ private Q_SLOTS:
void testConstexpr();
void testConversions();
void taskQTBUG_48780_pairContainingCArray();
void testDeducationRules();
};
class C { C() {} char _[4]; };
@ -202,5 +203,30 @@ void tst_QPair::taskQTBUG_48780_pairContainingCArray()
Q_UNUSED(pair);
}
void tst_QPair::testDeducationRules()
{
#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606
QPair p1{1, 2};
static_assert(std::is_same<decltype(p1)::first_type, decltype(1)>::value);
static_assert(std::is_same<decltype(p1)::second_type, decltype(2)>::value);
QCOMPARE(p1.first, 1);
QCOMPARE(p1.second, 2);
QPair p2{QString("string"), 2};
static_assert(std::is_same<decltype(p2)::first_type, QString>::value);
static_assert(std::is_same<decltype(p2)::second_type, decltype(2)>::value);
QCOMPARE(p2.first, "string");
QCOMPARE(p2.second, 2);
QPair p3(p2);
static_assert(std::is_same<decltype(p3)::first_type, decltype(p2)::first_type>::value);
static_assert(std::is_same<decltype(p3)::second_type, decltype(p2)::second_type>::value);
QCOMPARE(p3.first, "string");
QCOMPARE(p3.second, 2);
#else
QSKIP("Unsupported");
#endif
}
QTEST_APPLESS_MAIN(tst_QPair)
#include "tst_qpair.moc"