From: Pavel Pisa Date: Fri, 5 Sep 2014 18:12:28 +0000 (+0200) Subject: More portable and straightforward UL_CAST_UNQX implementation. X-Git-Url: https://rtime.felk.cvut.cz/gitweb/ulut.git/commitdiff_plain/f1961ed4b158bec3566b67887f9b5657129c0301?hp=6691271f8f3034e958ba6520f24d7ef9581a7030 More portable and straightforward UL_CAST_UNQX implementation. Signed-off-by: Pavel Pisa --- diff --git a/ulut/ul_utdefs.h b/ulut/ul_utdefs.h index adee9bd..4c89ca0 100644 --- a/ulut/ul_utdefs.h +++ b/ulut/ul_utdefs.h @@ -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(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)