On 12/11/17 17:12, Tom Lane wrote: > Peter Eisentraut <peter.eisentr...@2ndquadrant.com> writes: >> On 12/11/17 16:45, Tom Lane wrote: >>> (BTW, why is it that we can't fall back on the negative-width-bitfield >>> trick for old g++?) > >> The complaint is >> error: types may not be defined in 'sizeof' expressions > > Hmm, well, surely there's more than one way to do that; the sizeof > is just a convenient way to wrap it in C. Wouldn't a typedef serve > just as well?
Here is another attempt, which has the desired effect with the handful of compilers I have available. (With the recent changes to AllocSetContextCreate() using a static assertion, the current state now breaks actual extensions written in C++ in some configurations, so this has become a bit of a must-fix for PG11.) -- Peter Eisentraut http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From d631fcb1fb2babef618bc9b3eba3f5591970a609 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <pete...@gmx.net> Date: Tue, 30 Aug 2016 12:00:00 -0400 Subject: [PATCH v4] Add support for static assertions in C++ This allows modules written in C++ to use or include header files that use StaticAssertStmt() etc. --- src/include/c.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/include/c.h b/src/include/c.h index 11fcffbae3..22535a7deb 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -754,6 +754,7 @@ typedef NameData *Name; * 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. */ +#ifndef __cplusplus #ifdef HAVE__STATIC_ASSERT #define StaticAssertStmt(condition, errmessage) \ do { _Static_assert(condition, errmessage); } while(0) @@ -765,6 +766,19 @@ typedef NameData *Name; #define StaticAssertExpr(condition, errmessage) \ StaticAssertStmt(condition, errmessage) #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) \ + StaticAssertStmt(condition, errmessage) +#else +#define StaticAssertStmt(condition, errmessage) \ + do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0) +#define StaticAssertExpr(condition, errmessage) \ + ({ StaticAssertStmt(condition, errmessage); }) +#endif +#endif /* C++ */ /* base-commit: 56a95ee5118bf6d46e261b8d352f7dafac10587d -- 2.15.1