LWN.net Logo

The timer API: size or type safety?

The timer API: size or type safety?

Posted Dec 8, 2006 11:46 UTC (Fri) by viro (subscriber, #7872)
In reply to: The timer API: size or type safety? by pimlott
Parent article: The timer API: size or type safety?

Heh... The current version in my tree actually gets you a constant
arithmetic expression. It's built around the following:
#define check_callback_type(f,x) (sizeof((0 ? (f)(x) : (void)0), 0))
Feel free to torture students with it... It actually checks even
more - namely, that f returns void. And since it never evaluates
f or x, we are free to use it in macros without any concerns about
side effects. Can be used in global initializers, too (and yes,
it _is_ valid C99; we get past the constraints since the expression
that appears to be problematic is an argument of sizeof and doesn't
have a variably-modified type).


(Log in to post comments)

The timer API: size or type safety?

Posted Dec 8, 2006 17:10 UTC (Fri) by liljencrantz (subscriber, #28458) [Link]

That is so clever it hurts.

Am I correct in thinking that since check_callback_type never evaluates f or x, we also don't need to use typeof anymore, as we only truly reference f or x once? I.e. we can write this as:

check_callback_type( func, data );
timer->function = (void (*)(void *)) func;
timer->data = (void *) data;

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds