4 ms·
I have found this pretty handy for declaring a bunch of functions of all the same type, e.g. steps in a direct-threaded interpreter. typedef void Step(what
by mtklein 4y ago
I have found this pretty handy for declaring a bunch of functions of all the same type, e.g. steps in a direct-threaded interpreter.
typedef void Step(whatever...);
Step add,sub,mul,div,
load,store,
etc...;
- torstenvl 4y agoI've seen this used to define function pointers / callbacks in a "cleaner" way. I think Mg does this IIRC.
- mtklein 4y agoOh yes! I totally agree with that approach; I find it much clearer to typedef the function type rather than the function pointer type: typedef int (*CallbackPtr)(int,int); void foo(..., CallbackPtr callback); always reads less clearly to me than typedef int Callback(int,int); void foo(..., Callback* callback); I find this especially useful in C++ where the callback type could conceivably be some other type like std::function. Seeing that * helps me know at a glance it's probably just a plain old function pointer. Though I think maybe clearest of all is to not use a typedef, provided it doesn't cause other readability problems: void foo(..., int (*callback)(int,int)); (Not meaning to steal your thunder here... just wanted to write out an example in case anyone else was curious.)
- int_19h 4y agoAt that point you might as well typedef the function pointer type anyway, since it's just another *, and const/volatile variations for functions don't make sense.