Im interested to add in some extra logic around existing function calls, by wrapping them without renaming them. (just for a test).
The existin
For example, lets say I want to check the types on a function call.
A simple case, I want to check that sqrt
only takes (int
or double
), not float
.
Here is a method that works OK.
/* add this to a header or at the top of the source-code */
#include
static typeof(&sqrt) sqrt_wrap = sqrt;
#define sqrt(f) (_Generic((f), int: sqrt_wrap, double: sqrt_wrap))(f)
This works but has some drawbacks.
, or at least define sqrt
since we dont know if the static function is called or not.__attribute__((__unused__))
works in works in GCC/Clang.sqrt
.Edit!
This does in fact work, the header just needs to be included first (obviously - in retrospect)
#include
#define sqrt(f) _Generic((f), int: sqrt(f), double: sqrt(f))
Heres a trick to check sqrt
isn't called with float
and sqrtf
isn't called with double
#include
#define sqrt(X) _Generic((X), float: NULL, default: sqrt)(X)
#define sqrtf(X) _Generic((X), double: NULL, default: sqrtf)(X)