Long live qToUnderlying
"Cherry-pick" of C++2b's std::to_underlying. [ChangeLog][QtCore][QtGlobal] The qToUnderlying function has been added, to convert an value of enumeration type to its underlying value. Change-Id: Ia46bd8e4496e55174171ac2f0799eacbcca02cf9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
8b8ff64be2
commit
9a94c4a415
@ -3877,6 +3877,15 @@ bool qunsetenv(const char *varName)
|
|||||||
that qExchange() returns a non-const object, so Qt containers may detach.
|
that qExchange() returns a non-const object, so Qt containers may detach.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn template <typename Enum> std::underlying_type_t<Enum> qToUnderlying(Enum e)
|
||||||
|
\relates <QtGlobal>
|
||||||
|
\since 6.2
|
||||||
|
|
||||||
|
Converts the enumerator \e to the equivalent value expressed in its
|
||||||
|
enumeration's underlying type.
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\macro QT_TR_NOOP(sourceText)
|
\macro QT_TR_NOOP(sourceText)
|
||||||
\relates <QtGlobal>
|
\relates <QtGlobal>
|
||||||
|
@ -1177,6 +1177,13 @@ constexpr T qExchange(T &t, U &&newValue)
|
|||||||
return old;
|
return old;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// like std::to_underlying
|
||||||
|
template <typename Enum>
|
||||||
|
constexpr std::underlying_type_t<Enum> qToUnderlying(Enum e) noexcept
|
||||||
|
{
|
||||||
|
return static_cast<std::underlying_type_t<Enum>>(e);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __cpp_conditional_explicit
|
#ifdef __cpp_conditional_explicit
|
||||||
#define Q_IMPLICIT explicit(false)
|
#define Q_IMPLICIT explicit(false)
|
||||||
#else
|
#else
|
||||||
|
@ -60,6 +60,7 @@ private slots:
|
|||||||
void qRoundDoubles_data();
|
void qRoundDoubles_data();
|
||||||
void qRoundDoubles();
|
void qRoundDoubles();
|
||||||
void PRImacros();
|
void PRImacros();
|
||||||
|
void testqToUnderlying();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" { // functions in qglobal.c
|
extern "C" { // functions in qglobal.c
|
||||||
@ -698,5 +699,24 @@ void tst_QGlobal::PRImacros()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QGlobal::testqToUnderlying()
|
||||||
|
{
|
||||||
|
enum class E {
|
||||||
|
E1 = 123,
|
||||||
|
E2 = 456,
|
||||||
|
};
|
||||||
|
static_assert(std::is_same_v<decltype(qToUnderlying(E::E1)), int>);
|
||||||
|
QCOMPARE(qToUnderlying(E::E1), 123);
|
||||||
|
QCOMPARE(qToUnderlying(E::E2), 456);
|
||||||
|
|
||||||
|
enum EE : unsigned long {
|
||||||
|
EE1 = 123,
|
||||||
|
EE2 = 456,
|
||||||
|
};
|
||||||
|
static_assert(std::is_same_v<decltype(qToUnderlying(EE1)), unsigned long>);
|
||||||
|
QCOMPARE(qToUnderlying(EE1), 123UL);
|
||||||
|
QCOMPARE(qToUnderlying(EE2), 456UL);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(tst_QGlobal)
|
QTEST_APPLESS_MAIN(tst_QGlobal)
|
||||||
#include "tst_qglobal.moc"
|
#include "tst_qglobal.moc"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user