Can I mimic a C header that redefines bool in C++?
Solution 1:
You can hack it!
The library, call it fooLib
, thinks it's using some type bool
which it has the prerogative to define. To the library, bool
is just an identifier.
So, you can just force it to use another identifier instead:
#define bool fooLib_bool
#include "fooLib.h"
#undef bool
#undef true
#undef false
Now the compiler sees the offending line transformed to this:
typedef int fooLib_bool;
You're stuck with the interface using type fooLib_bool = int
instead of a real bool
, but that's impossible to work around, as the code might in fact rely on the properties of int
, and library binary would have been compiled with such an assumption baked in.
Solution 2:
I suppose you can wrap the offending code into a header and then undef what you don't need
Library_wrapper.h:
#define bool something_else // This will get you past the C++ compilation
#include "library.h"
#undef false
#undef true
#undef bool
main.cpp:
#include "Library_wrapper.h"
#include "boost.h"
Regarding the typedef.. the compiler should complain if you try to redefine a basic type in C++. You can redeclare a type by the way (it is allowed in C++) or define it (simple text replacement).