diff options
-rw-r--r-- | src/include/c.h | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/include/c.h b/src/include/c.h index 6558801e5ff..831c89f473d 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -836,37 +836,43 @@ extern void ExceptionalCondition(const char *conditionName, * The macro StaticAssertDecl() is suitable for use at file scope (outside of * any function). * - * On recent C++ compilers, we can use standard static_assert(). - * * Otherwise we fall back on a kluge that assumes the compiler will complain * about a negative width for a struct bit-field. This will not include a * helpful error message, but it beats not getting an error at all. */ -#if !defined(__cplusplus) && defined(HAVE__STATIC_ASSERT) -/* Default C implementation */ +#ifndef __cplusplus +#ifdef HAVE__STATIC_ASSERT #define StaticAssertStmt(condition, errmessage) \ do { _Static_assert(condition, errmessage); } while(0) #define StaticAssertExpr(condition, errmessage) \ ((void) ({ StaticAssertStmt(condition, errmessage); true; })) #define StaticAssertDecl(condition, errmessage) \ _Static_assert(condition, errmessage) -#elif defined(__cplusplus) && __cpp_static_assert >= 200410 -/* Default C++ implementation */ +#else /* !HAVE__STATIC_ASSERT */ +#define StaticAssertStmt(condition, errmessage) \ + ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) +#define StaticAssertExpr(condition, errmessage) \ + StaticAssertStmt(condition, errmessage) +#define StaticAssertDecl(condition, errmessage) \ + extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1]) +#endif /* HAVE__STATIC_ASSERT */ +#else /* C++ */ +#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410 #define StaticAssertStmt(condition, errmessage) \ static_assert(condition, errmessage) #define StaticAssertExpr(condition, errmessage) \ ({ static_assert(condition, errmessage); }) #define StaticAssertDecl(condition, errmessage) \ static_assert(condition, errmessage) -#else -/* Fallback implementation for C and C++ */ +#else /* !__cpp_static_assert */ #define StaticAssertStmt(condition, errmessage) \ - ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) + do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0) #define StaticAssertExpr(condition, errmessage) \ - StaticAssertStmt(condition, errmessage) + ((void) ({ StaticAssertStmt(condition, errmessage); })) #define StaticAssertDecl(condition, errmessage) \ extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1]) -#endif +#endif /* __cpp_static_assert */ +#endif /* C++ */ /* |