]> rtime.felk.cvut.cz Git - ulut.git/commitdiff
More portable and straightforward UL_CAST_UNQX implementation.
authorPavel Pisa <pisa@cmp.felk.cvut.cz>
Fri, 5 Sep 2014 18:12:28 +0000 (20:12 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Fri, 5 Sep 2014 18:12:28 +0000 (20:12 +0200)
Signed-off-by: Pavel Pisa <pisa@cmp.felk.cvut.cz>
ulut/ul_utdefs.h

index adee9bda5443d5eece4e343946949b42a61a14fb..4c89ca0e0424badc9a4db4af48b3dc248a4f43d5 100644 (file)
@@ -150,26 +150,30 @@ extern "C" {
 #define UL_TYPEOF_REFX(ref_asterisks, ptr_type) \
   typeof(ref_asterisks(union { int z; typeof(ptr_type) x; }){0}.x)
 
-#ifdef __GNUC__
-/* Const and volatile qualifiers removal cast
- * The expression is cast by (typeof((typeof(new_type[1])){0}[0]))
- * which is equivalent to (new_type) cast for matching types.
- * This quite complicated method is used because both comma operator
- * and compound expressions are not allowed in global variables
- * initializers.
- */
+/* Const and volatile qualifiers removal cast */
+#ifdef __cplusplus
+#define UL_CAST_UNQX(ref_asterisks, new_type, expr) \
+            (const_cast<new_type>(expr))
+#else /* Standard C code */
+#ifdef __GNUC__
+#if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
+extern void* UL_CAST_UNQX_types_not_compatible(void)
+  __attribute__((error ("UL_CAST_UNQX types differ not only by volatile and const")));
+#else
+extern void UL_CAST_UNQX_types_not_compatible(void);
+#endif
 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ( \
-  (typeof((typeof(new_type[1 - 2 * \
-    !__builtin_types_compatible_p \
+  __builtin_choose_expr(__builtin_types_compatible_p \
     (UL_TYPEOF_REFX(ref_asterisks, expr), \
-     UL_TYPEOF_REFX(ref_asterisks, new_type)) \
-    ])){0}[0]) \
+     UL_TYPEOF_REFX(ref_asterisks, new_type)), \
+    (new_type)(expr), \
+    UL_CAST_UNQX_types_not_compatible() \
   ) \
-  (expr) \
 )
 #else /*__GNUC__*/
 #define UL_CAST_UNQX(ref_asterisks, new_type, expr) ((new_type)(expr))
 #endif /*__GNUC__*/
+#endif /*__cplusplus*/
 
 #define UL_CAST_UNQ1(new_type, expr) \
   UL_CAST_UNQX(*, new_type, expr)