#ifndef TYPECHECK_H_INCLUDED #define TYPECHECK_H_INCLUDED /* * 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; \ }) /* * Check at compile time that 'function' is a certain type, or is a pointer * to that type (needs to use typedef for the function type.) */ #define typecheck_fn(type,function) \ ({ typeof(type) __tmp = function; \ (void)__tmp; \ }) #endif /* TYPECHECK_H_INCLUDED */
typecheck(type, x)用於檢查x是不是"type"的資料型態,如果不是,compiler會出現warning: comparison of distinct pointer types lacks a cast提醒programmer。

typecheck_fn(type, function)用於檢查"function"是不是和"type"有相同的資料型態,如果不是,compiler會出現warning: initialization from incompatible pointer type提醒programmer。

沒有留言:
張貼留言