summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@kde.org>2011-11-02 10:26:50 +0100
committerQt by Nokia <qt-info@nokia.com>2011-11-02 13:24:36 +0100
commitf3242864c3e8384aa1153a9044143265f3af0b62 (patch)
tree6f0ad82778437b2934b1ff66625f685baf83b89f
parentca25b63bea24098bd43a31dd07d105ce17b8069b (diff)
Use C++11 static_assert
Change-Id: I75aa2bc209cdc8869e7daa9fd0dd865ccf65a68e Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Olivier Goffart <ogoffart@kde.org>
-rw-r--r--src/corelib/global/qglobal.h10
-rw-r--r--tests/auto/corelib/global/qglobal/tst_qglobal.cpp17
2 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h
index 9d3c1be4b1..1bc93376ee 100644
--- a/src/corelib/global/qglobal.h
+++ b/src/corelib/global/qglobal.h
@@ -424,6 +424,7 @@ namespace QT_NAMESPACE {}
# define Q_COMPILER_AUTO_TYPE
# define Q_COMPILER_LAMBDA
# define Q_COMPILER_DECLTYPE
+# define Q_COMPILER_STATIC_ASSERT
// MSCV has std::initilizer_list, but do not support the braces initialization
//# define Q_COMPILER_INITIALIZER_LISTS
# endif
@@ -518,6 +519,7 @@ namespace QT_NAMESPACE {}
/* C++0x features supported in GCC 4.3: */
# define Q_COMPILER_RVALUE_REFS
# define Q_COMPILER_DECLTYPE
+# define Q_COMPILER_STATIC_ASSERT
# endif
# if (__GNUC__ * 100 + __GNUC_MINOR__) >= 404
/* C++0x features supported in GCC 4.4: */
@@ -800,6 +802,7 @@ namespace QT_NAMESPACE {}
# define Q_COMPILER_DEFAULT_DELETE_MEMBERS
# define Q_COMPILER_CLASS_ENUM
# define Q_COMPILER_LAMBDA
+# define Q_COMPILER_STATIC_ASSERT
# endif
# endif
#endif
@@ -1723,6 +1726,11 @@ Q_CORE_EXPORT void qt_assert_x(const char *where, const char *what, const char *
# endif
#endif
+
+#ifdef Q_COMPILER_STATIC_ASSERT
+#define Q_STATIC_ASSERT(Condition) static_assert(Condition, #Condition)
+#define Q_STATIC_ASSERT_X(Condition, Message) static_assert(Condition, Message)
+#else
// Intentionally undefined
template <bool Test> class QStaticAssertFailure;
template <> class QStaticAssertFailure<true> {};
@@ -1731,6 +1739,8 @@ template <> class QStaticAssertFailure<true> {};
#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<bool(Condition)>)}
+#define Q_STATIC_ASSERT_X(Condition, Message) Q_STATIC_ASSERT(Condition)
+#endif
Q_CORE_EXPORT void qt_check_pointer(const char *, int);
Q_CORE_EXPORT void qBadAlloc();
diff --git a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
index 9c6e5ced47..c0a14faac3 100644
--- a/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
+++ b/tests/auto/corelib/global/qglobal/tst_qglobal.cpp
@@ -278,14 +278,20 @@ public:
{
Q_STATIC_ASSERT(true);
Q_STATIC_ASSERT(!false);
+ Q_STATIC_ASSERT_X(true,"");
+ Q_STATIC_ASSERT_X(!false,"");
}
~MyTrue()
{
Q_STATIC_ASSERT(true);
Q_STATIC_ASSERT(!false);
+ Q_STATIC_ASSERT_X(true,"");
+ Q_STATIC_ASSERT_X(!false,"");
}
Q_STATIC_ASSERT(true);
Q_STATIC_ASSERT(!false);
+ Q_STATIC_ASSERT_X(true,"");
+ Q_STATIC_ASSERT_X(!false,"");
};
struct MyExpresion
@@ -294,16 +300,21 @@ struct MyExpresion
{
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
+ Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0,"");
+ Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0,"");
}
private:
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
Q_STATIC_ASSERT(sizeof(MyTrue) > 0);
+ Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0, "");
+ Q_STATIC_ASSERT_X(sizeof(MyTrue) > 0, "");
};
struct TypeDef
{
typedef int T;
Q_STATIC_ASSERT(sizeof(T));
+ Q_STATIC_ASSERT_X(sizeof(T), "");
};
template<typename T1, typename T2>
@@ -315,6 +326,10 @@ struct Template
Q_STATIC_ASSERT(!!True);
Q_STATIC_ASSERT(sizeof(DependentType));
Q_STATIC_ASSERT(!!sizeof(DependentType));
+ Q_STATIC_ASSERT_X(True, "");
+ Q_STATIC_ASSERT_X(!!True, "");
+ Q_STATIC_ASSERT_X(sizeof(DependentType), "");
+ Q_STATIC_ASSERT_X(!!sizeof(DependentType), "");
};
struct MyTemplate
@@ -322,6 +337,8 @@ struct MyTemplate
static const bool Value = Template<TypeDef, int>::True;
Q_STATIC_ASSERT(Value);
Q_STATIC_ASSERT(!!Value);
+ Q_STATIC_ASSERT_X(Value, "");
+ Q_STATIC_ASSERT_X(!!Value, "");
};
void tst_QGlobal::qstaticassert()