Re: Refactor compile-time assertion checks for C/C++ - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Refactor compile-time assertion checks for C/C++
Date
Msg-id 5592.1584412505@sss.pgh.pa.us
Whole thread Raw
In response to Re: Refactor compile-time assertion checks for C/C++  (Michael Paquier <michael@paquier.xyz>)
Responses Re: Refactor compile-time assertion checks for C/C++  (Michael Paquier <michael@paquier.xyz>)
List pgsql-hackers
Michael Paquier <michael@paquier.xyz> writes:
> On Mon, Mar 16, 2020 at 10:32:36AM -0400, Tom Lane wrote:
>> Sorry for being unclear --- I just meant that we could use do{}
>> in StaticAssertStmt for both C and C++.  Although now I notice
>> that the code is trying to use StaticAssertStmt for StaticAssertExpr,
>> which you're right isn't going to do.  But I think something like
>> this would work and be a bit simpler than what you proposed:
>>
>> #else
>> /* Fallback implementation for C and C++ */
>> #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) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
>> #define StaticAssertDecl(condition, errmessage) \

> C++ does not allow defining a struct inside a sizeof() call, so in
> this case StaticAssertExpr() does not work with the previous extension
> in C++.  StaticAssertStmt() does the work though.

[ scratches head... ]  A do{} is okay in an expression in C++ ??

            regards, tom lane



pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: Add PostgreSQL home page to --help output
Next
From: Thunder
Date:
Subject: Re:Standby got fatal after the crash recovery