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

Поиск
Список
Период
Сортировка
От Michael Paquier
Тема Re: Refactor compile-time assertion checks for C/C++
Дата
Msg-id 20200317020613.GA2206@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: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.

One alternatine I can think of for C++ would be something like the
following, though C does not like this flavor either:
typedef char static_assert_struct[condition ? 1 : -1]
--
Michael

Вложения

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

Предыдущее
От: Justin Pryzby
Дата:
Сообщение: Re: pg11+: pg_ls_*dir LIMIT 1: temporary files .. not closed atend-of-transaction
Следующее
От: Michael Paquier
Дата:
Сообщение: Re: Add PostgreSQL home page to --help output