Re: Refactor compile-time assertion checks for C/C++

Поиск
Список
Период
Сортировка
От Michael Paquier
Тема Re: Refactor compile-time assertion checks for C/C++
Дата
Msg-id 20200317041156.GA112208@paquier.xyz
обсуждение исходный текст
Ответ на Re: Refactor compile-time assertion checks for C/C++  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: Refactor compile-time assertion checks for C/C++  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-hackers
On Mon, Mar 16, 2020 at 10:35:05PM -0400, Tom Lane wrote:
> Michael Paquier <michael@paquier.xyz> writes:
>> 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++ ??

cpp-fallback-fix.patch in [1] was doing that.

The fun does not stop here.  gcc is fine when using that for C and
C++:
#define StaticAssertStmt(condition, errmessage) \
   do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
#define StaticAssertExpr(condition, errmessage) \
   ((void) ({ StaticAssertStmt(condition, errmessage); }))

But then problems come from MSVC which does not like the do{} part for
statements, and this works:
#define StaticAssertStmt(condition, errmessage) \
    ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
#define StaticAssertExpr(condition, errmessage) \
    StaticAssertStmt(condition, errmessage)

[1]: https://postgr.es/m/20200313115033.GA183471@paquier.xyz
--
Michael

Вложения

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Thunder
Дата:
Сообщение: Re:Re: Re:Standby got fatal after the crash recovery
Следующее
От: Amit Kapila
Дата:
Сообщение: Re: error context for vacuum to include block number