3 ms·
Another advantage of typedefing the function is that you can let the compiler help ensure your function definition signatures are correct. For example, if you
by jerryr 10y ago
Another advantage of typedefing the function is that you can let the compiler help ensure your function definition signatures are correct.
For example, if you typedef a callback type like so:
typedef int callback_t(int foo);
And then declare a callback like so:
callback_t my_callback;
And then later define the callback like so:
int my_callback(int foo) {
...
}
The compiler will produce an error if you screw up the function signature of my_callback() when defining it because it won't match the prototype you defined via the typedef. This only works in C. C++ allows multiple function signatures for the same function name, so you probably won't get a compiler error--though you'll likely wind up with a linker error.
Edit: The downside is that function declarations done this way will look a little odd--possibly mistaken for a variable definition. And, upon further thought, I'm not sure that this pattern really is a huge benefit since function pointer assignment will also produce an error if the signatures don't match. But interesting nonetheless, I guess.