Use C++11 static_assert
authorOlivier Goffart <ogoffart@kde.org>
Wed, 2 Nov 2011 09:26:50 +0000 (10:26 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 2 Nov 2011 12:24:36 +0000 (13:24 +0100)
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>
src/corelib/global/qglobal.h
tests/auto/corelib/global/qglobal/tst_qglobal.cpp

index 9d3c1be..1bc9337 100644 (file)
@@ -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();
index 9c6e5ce..c0a14fa 100644 (file)
@@ -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()