Short live Q_NODISCARD_(CTOR_)X!
Wrappers around P1301 [[nodiscard("reason")]].
[ChangeLog][QtCore][Q_NODISCARD_X/Q_NODISCARD_CTOR_X] Added as
wrappers around C++20 [[nodiscard("reason")]].
Task-number: QTBUG-114767
Change-Id: Ie566d9c9d500ef632c7e243af97081f83506a752
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
bb10
parent
1a9ba8ee7d
commit
38cd3cb126
|
|
@ -959,6 +959,13 @@
|
|||
# ifndef Q_NODISCARD_CTOR
|
||||
# define Q_NODISCARD_CTOR [[nodiscard]]
|
||||
# endif
|
||||
// [[nodiscard("reason")]] (P1301)
|
||||
# ifndef Q_NODISCARD_X
|
||||
# define Q_NODISCARD_X(message) [[nodiscard(message)]]
|
||||
# endif
|
||||
# ifndef Q_NODISCARD_CTOR_X
|
||||
# define Q_NODISCARD_CTOR_X(message) [[nodiscard(message)]]
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#if __has_cpp_attribute(maybe_unused)
|
||||
|
|
@ -1010,9 +1017,15 @@
|
|||
#ifndef Q_REQUIRED_RESULT
|
||||
# define Q_REQUIRED_RESULT
|
||||
#endif
|
||||
#ifndef Q_NODISCARD_X
|
||||
# define Q_NODISCARD_X(message) Q_REQUIRED_RESULT
|
||||
#endif
|
||||
#ifndef Q_NODISCARD_CTOR
|
||||
# define Q_NODISCARD_CTOR
|
||||
#endif
|
||||
#ifndef Q_NODISCARD_CTOR_X
|
||||
# define Q_NODISCARD_CTOR_X(message) Q_NODISCARD_CTOR
|
||||
#endif
|
||||
#ifndef Q_DECL_DEPRECATED
|
||||
# define Q_DECL_DEPRECATED
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -409,4 +409,19 @@
|
|||
implement the feature. If your supported platforms all allow \c{[[nodiscard]]}
|
||||
on constructors, we strongly recommend you use the C++ attribute directly instead
|
||||
of this macro.
|
||||
|
||||
\sa Q_NODISCARD_CTOR_X
|
||||
*/
|
||||
|
||||
/*!
|
||||
\macro Q_NODISCARD_X(message)
|
||||
\macro Q_NODISCARD_CTOR_X(message)
|
||||
\relates <QtCompilerDetection>
|
||||
\since 6.7
|
||||
|
||||
\brief Expand to \c{[[nodiscard(message)]]} on compilers that support the feature.
|
||||
|
||||
Otherwise they expand to [[nodiscard]] and Q_NODISCARD_CTOR, respectively.
|
||||
|
||||
\sa`Q_NODISCARD_CTOR
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ private slots:
|
|||
void qRoundDoubles();
|
||||
void PRImacros();
|
||||
void testqToUnderlying();
|
||||
void nodiscardConstructor();
|
||||
void nodiscard();
|
||||
};
|
||||
|
||||
extern "C" { // functions in qglobal.c
|
||||
|
|
@ -692,16 +692,19 @@ void tst_QGlobal::testqToUnderlying()
|
|||
QCOMPARE(qToUnderlying(EE2), 456UL);
|
||||
}
|
||||
|
||||
void tst_QGlobal::nodiscardConstructor()
|
||||
void tst_QGlobal::nodiscard()
|
||||
{
|
||||
// Syntax-only test, just to make sure that Q_NODISCARD_CTOR compiles
|
||||
// Syntax-only test, just to make sure that the Q_NODISCARD_* compile
|
||||
// on all platforms.
|
||||
// Other code is just to silence all various compiler warnings about
|
||||
// unused private members or methods.
|
||||
class Test {
|
||||
public:
|
||||
Q_NODISCARD_CTOR explicit Test(int val) : m_val(val) {}
|
||||
Q_NODISCARD_CTOR_X("Why construct a Test instead of just passing the int through?")
|
||||
explicit Test(int val) : m_val(val) {}
|
||||
Q_NODISCARD_CTOR explicit Test(float val) : m_val(int(val)) {}
|
||||
|
||||
Q_NODISCARD_X("Why call get() if you don't use the returned value, hu?") // NOT idiomatic use!
|
||||
int get() const { return m_val; }
|
||||
|
||||
private:
|
||||
|
|
@ -710,6 +713,8 @@ void tst_QGlobal::nodiscardConstructor()
|
|||
|
||||
Test t{42};
|
||||
QCOMPARE(t.get(), 42);
|
||||
Test t2{42.0f};
|
||||
QCOMPARE(t2.get(), 42);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QGlobal)
|
||||
|
|
|
|||
Loading…
Reference in New Issue