Thread (6 messages) 6 messages, 5 authors, 2011-01-31

typecheck code

From: Manish Katiyar <hidden>
Date: 2011-01-31 18:13:20
Also in: lkml

On Mon, Jan 31, 2011 at 9:03 AM, Sri Ram Vemulpali
[off-list ref] wrote:
Hi all,

/*
?* Check at compile time that something is of a particular type.
?* Always evaluates to 1 so you may use it easily in comparisons.
?*/
?#define typecheck(type,x) \
?({ ? ? ?type __dummy; \
? ? ? ?typeof(x) __dummy2; \
? ? ? ?(void)(&__dummy == &__dummy2); \
? ? ? ?1; \
?})

#define typecheck_fn(type,function) \
({ ? ? ?typeof(type) __tmp = function; \
? ? ? (void)__tmp; \
})

Can anyone help me, explain the above code typecheck. How does
(void)(&__dummy == &__dummy2) evaluates to 1
Infact I think it will never return 1, since the addresses of __dummy1
and __dummy2 have to be different (off by 4 or 8). As pointed out it
is the next line that always returns 1. The purpose of this line is to
throw away warnings like "Incompatible pointer comparison" or
something like that (haven't tried :-)) incase there is a mismatch.

-- 
Thanks -
Manish
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help