简体   繁体   中英

Replacing functions in the std namespace

I have a library that replaces free , malloc and a couple of other functions by using a define in it's header:

#define free some_custom_free 

This unfortunately horribly breaks when other libraries (boost in this case) use std::free instead of directly calling free :

error: 'some_custom_free' is not a member of 'std'

Can this be properly and portably solved, preferably without having to touch either libraries?

Nope. You're screwed a thousand ways from Sunday because the library that used #define was written by an Australopithecus. The only solution is to change the library that did #define free .

You SHOULD use __malloc_hook , which allows you to change what malloc does.

  • set __malloc_hook to actaully run some_custom_malloc

  • set __free_hook to run some_custom_free

  • remove the #define and everything will work fine!

Another option - make sure the #include for this library are always AFTER the #include for boost / stl / whatever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM