Open noncombatant opened 7 years ago
I believe you, but available evidence suggests that you are nearly the only person in the world who writes function pointer typedefs this way. Do you know which version of the syntax they appeared in?
I don't know what version that syntax became legal in; AFAIK, it has always been legal. I can build it without warnings in C 89 mode on both clang and GCC:
$ cat f.c
typedef int goat_func(int goat);
int f(int goat) {
return goat + 42;
}
int main() {
goat_func* x = f;
return x(1);
}
$ CC=gcc make f
gcc -Wall -Wextra -Werror -std=c89 -ansi -pedantic -O0 f.c -o f
$ rm f && CC=clang make f
clang -Wall -Wextra -Werror -std=c89 -ansi -pedantic -O0 f.c -o f
$
On Tue, Sep 12, 2017 at 10:29 PM, Adam Bard notifications@github.com wrote:
I believe you, but available evidence suggests that you are nearly the only person in the world who writes function pointer typedefs this way. Do you know which version of the syntax they appeared in?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/adambard/learnxinyminutes-docs/issues/2850#issuecomment-329063179, or mute the thread https://github.com/notifications/unsubscribe-auth/AAWv1cbaBpcQ_QUe1Q4ts0_0hAkcu4e9ks5sh2grgaJpZM4PSeBw .
This is legal, and IMHO more understandable:
As I'll show below, this syntax is unnecessarily hairy as well:
I.e. the alternative syntax is less nasty. Why bother with the nasty flavor? :)
The only quirk is, when putting such a function type as a member of a
struct
, you do need to give it a*
:You don't need the
*
when passing such functiontypedef
s to other functions. Just forstruct
members. I find this greatly improves the readability of function types, which is good, since they're so useful. The unnecessary ugliness of the classic style makes me sad. :)Also I find the named
struct
member initializers improve readability (and I wish C++ had them).