I have a series of functions with the same prototype, say
int func1(int a, int b) {
// ...
}
int func2(int a, int b) {
// ...
}
// ...
Now, I want to simplify their definition and declaration. Of course I could use a macro like that:
#define SP_FUNC(name) int name(int a, int b)
But I'd like to keep it in C, so I tried to use the storage specifier typedef
for this:
typedef int SpFunc(int a, int b);
This seems to work fine for the declaration:
SpFunc func1; // compiles
but not for the definition:
SpFunc func1 {
// ...
}
which gives me the following error:
error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
Is there a way to do this correctly or is it impossible?
To my understanding of C this should work, but it doesn't. Why?
Note, gcc understands what I am trying to do, because, if I write
SpFunc func1 = { /* ... */ }
it tells me
error: function 'func1' is initialized like a variable
Which means that gcc understands that SpFunc is a function type.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…