From 85031658d19d68409a6eff4be2fe5d326b98bb7c Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Sun, 1 Jul 2012 21:27:11 +0200 Subject: [PATCH] Add Q_STATIC_ASSERT macro, backported from Qt 5. --- src/core/Global.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/core/Global.h b/src/core/Global.h index 3f3ad3b43..56068714e 100644 --- a/src/core/Global.h +++ b/src/core/Global.h @@ -42,6 +42,9 @@ # if __has_feature(cxx_nullptr) # define COMPILER_NULLPTR # endif +# if __has_feature(cxx_static_assert) +# define COMPILER_STATIC_ASSERT +# endif # endif #endif // Q_CC_CLANG @@ -50,6 +53,7 @@ # if (__GNUC__ * 100 + __GNUC_MINOR__) >= 403 /* C++11 features supported in GCC 4.3: */ # define COMPILER_DECLTYPE +# define COMPILER_STATIC_ASSERT # endif # if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404 # define COMPILER_CLASS_ENUM @@ -99,4 +103,21 @@ # endif #endif +#if !defined(Q_STATIC_ASSERT) && !defined(Q_STATIC_ASSERT_X) +#ifdef COMPILER_STATIC_ASSERT +#define Q_STATIC_ASSERT(Condition) static_assert(bool(Condition), #Condition) +#define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message) +#else +// Intentionally undefined +template class QStaticAssertFailure; +template <> class QStaticAssertFailure {}; + +#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 +#define Q_STATIC_ASSERT(Condition) \ + enum {Q_STATIC_ASSERT_PRIVATE_JOIN(q_static_assert_result, __LINE__) = sizeof(QStaticAssertFailure)} +#define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition) +#endif // COMPILER_STATIC_ASSERT +#endif // !defined(Q_STATIC_ASSERT) && !defined(Q_STATIC_ASSERT_X) + #endif // KEEPASSX_GLOBAL_H