Q_STATIC_ASSERT: use __COUNTER__ instead of __LINE__ if the compiler supports it

When using __LINE__ to construct unique names, use of Q_STATIC_ASSERT
is limited to one instance per line of code. On compilers that support
__COUNTER__ (GCC and MSVC, probably others), we can get around that
limitation by using that one to always get a new unique number, so
use it.

Change-Id: I89bcfaa32376b7a665f03e4275e89b13fa3e650d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2013-09-22 05:17:22 +02:00 committed by The Qt Project
parent 1b34df6ea6
commit ab55f9f39b
2 changed files with 10 additions and 0 deletions

View File

@ -656,8 +656,13 @@ template <> class QStaticAssertFailure<true> {};
#define Q_STATIC_ASSERT_PRIVATE_JOIN(A, B) Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B)
#define Q_STATIC_ASSERT_PRIVATE_JOIN_IMPL(A, B) A ## B
#ifdef __COUNTER__
#define Q_STATIC_ASSERT(Condition) \
enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __COUNTER__) = sizeof(QStaticAssertFailure<!!(Condition)>)}
#else
#define Q_STATIC_ASSERT(Condition) \
enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) = sizeof(QStaticAssertFailure<!!(Condition)>)}
#endif /* __COUNTER__ */
#define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
#endif

View File

@ -290,6 +290,11 @@ void tst_QGlobal::qstaticassert()
Q_UNUSED(tmp1);
Q_UNUSED(tmp2);
Q_UNUSED(tmp3);
#ifdef __COUNTER__
// if the compiler supports __COUNTER__, multiple
// Q_STATIC_ASSERT's on a single line should compile:
Q_STATIC_ASSERT(true); Q_STATIC_ASSERT_X(!false, "");
#endif // __COUNTER__
QVERIFY(true); // if the test compiles it has passed.
}