Re: style for typedef of function that will be pointed to

From: Chapman Flack <chap(at)anastigmatix(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: style for typedef of function that will be pointed to
Date: 2022-02-07 21:58:21
Message-ID: 620195FD.6060900@anastigmatix.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 10/05/21 14:00, Chapman Flack wrote:
> On 10/05/21 13:47, Tom Lane wrote:
>>> An alternative I've sometimes used elsewhere is to typedef the function
>>> type itself, and use the * when declaring a pointer to it:
>>> typedef void Furbinator(char *furbee);
>>
>> Is that legal C? I doubt that it was before C99 or so. As noted
>> in the Ghostscript docs you came across, it certainly wouldn't have
>> been portable back in the day.
>
> It compiles silently for me with gcc --std=c89 -Wpedantic
>
> I think that's the oldest standard I can ask gcc about. Per the manpage,
> 'c89' is ISO C90 without its amendment 1, and without any gnuisms.

There are some places in the tree where AssertVariableIsOfType is being
cleverly used to achieve the same thing:

void
_PG_output_plugin_init(OutputPluginCallbacks *cb)
{
AssertVariableIsOfType(&_PG_output_plugin_init, LogicalOutputPluginInit);

void
_PG_archive_module_init(ArchiveModuleCallbacks *cb)
{
AssertVariableIsOfType(&_PG_archive_module_init, ArchiveModuleInit);

While clever, doesn't it seem like a strained way to avoid just saying:

typedef void ArchiveModuleInit(ArchiveModuleCallbacks *cb);

ArchiveModuleInit _PG_archive_module_init;

void
_PG_archive_module_init(ArchiveModuleCallbacks *cb)
{

if indeed compilers C90 and later are happy with the straight typedef?

Not that one would go changing existing declarations. But perhaps it
could be on the table for new ones?

Regards,
-Chap

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Stark 2022-02-07 22:00:32 WaitLatchOrSocket seems to not count to 4 right...
Previous Message Tom Lane 2022-02-07 21:30:53 Re: [RFC] building postgres with meson - autogenerated headers