How does the C++ compiler decide which function/method to call if there are multiple possibilities? In my specific case I have the standard free function of the C++ Run time and I also have a templated free variant, like this:
// The definitions of the C++ Run Time Library (from memory.h)
extern malloc(size_t s);
extern void free(void *p);
// Our own memory management functions
extern void *OurMalloc(size_t s);
extern void OurFree(void *p);
// Own variants to overrule malloc and free (instead of using #define)
template<typename T>
void *malloc(T t)
{
return OurMalloc(t);
}
template<typename T>
void free(T *t)
{
OurFree(t);
}
I tested this using the following code:
void main(void)
{
void *p = malloc(10);
free(p);
}
If I compile and run this, it seems that the call to malloc is correctly replaced by the templated variant. So far, so good.
However, the call to free is not replaced by the templated variant, and the standard C++ function is still called.
What rules does the C++ compiler use to decide which variant to give priority? Is this related to the Koenig-lookup rules?
Note: I tried this alternative because using #define does not solve the problem (see question http://stackoverflow.com/questions/2319381).