Kenneth said:
Ian said:
Pietro said:
Hi Group,
is there a mean to undefine a function, in a similar way as you can
undefine macros?
For example, let's say that I need a few declarations from stdio.h but
want to define my own printf:
[...]
No, once a symbol is declared in a compilation unit, it's definition can
not be changed.
The best you can do is something gross like:
#define puts _puts
#include <stdio.h>
#undef puts
void puts( int n ) {}
int main(void) {
puts( 42 );
}
Or:
#include <stdio.h>
#undef puts
#define puts MyPuts
...
I think this is the best you can do, for all the best
definitions of "best." It's still not perfect. One problem
is that it doesn't affect puts() calls in modules you don't
have an opportunity to recompile, like those in libraries
you use only in pre-compiled form.
Another problem is illustrated by a conversation I had
with a colleague at a PPOE. He'd spent some time as a compiler
developer, and told me that the compiler he worked on replaced
`printf ("Hello, world!\n")' with `puts ("Hello, world")'. It
seems one of the SPEC benchmarks involved a lot of printf() calls
with no "%" directives, so this transformation got them a better
SPEC score ...
Anyhow, a question arises: He told me they replaced the
call to printf() with a call to puts(), but he didn't tell me
exactly how that call was expressed and implemented. Perhaps
the substitution occurred long after the preprocessor operated,
so the macro dodge above would fail to intercept the call. Or
perhaps the substitution *was* subject to macro replacement, in
which case the dodge above would wind up handling some printf()
calls in addition to the explicit puts() calls it was intended
to snaffle ... Either way, the "best you can do" macro dodge
is on thin ice.
Besides, a strict reading of 7.1.3p1 says `#undef puts'
and `#define puts ...' invoke undefined behavior.