Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

So I have always used the following idiom to get around collisions when I must include two header files that define a type or function with the same name and I am unable to alter them for one reason or another:

// SomeFile.h
typedef signed char int8

// SomeOtherFile.h
typedef char int8

// some cpp file
#include "SomeFile.h"

namespace SomeNameSpace
{
    #include SomeOtherFile.h
}

using SomeNameSpace::SomeFunction;
// ...

I've never put much thought into it, but is there a better way to workaround this issue other than what I propose above? It hasn't caused me any problems, but as I just had to do it again it got me wondering. Anyway, thanks in advance.

share|improve this question
    
Are both files external to your company? –  Loki Astari Sep 7 '11 at 23:52
    
Sorry, yes, I should have mentioned that; I cannot change the header files in any way. –  Ed S. Sep 8 '11 at 0:05

1 Answer 1

up vote 1 down vote accepted

There is always the C function alias way. In a source file

#include "SomeOtherFile.h"

create another header file (e.g. MySomeOtherFile.h" with new names for the conflicting functions. e.g.

void MySomeFunction();

Then implement it as a forwarder to the original. e.g.

void MySomeFunction() { SomeFunction(); }

No conflicts, no namespaces. Unfortunately the price is an extra function call, an extra header and source file and tedious forwarding routines to write.

You asked is there better way? This is the another way to solve this problem, not a better way though. There is another way but I would not say it's much better either, where you use a inline functions to call global function pointers initialised to the address(es) of the clashing functions. e.g. in a header file

typedef void (*SomeFunctionPtr)();
SomeFunctionPtr pSomeFunction;
inline void MyFunction() { if (pSomeFunction) (*pSomeFunction)(); }

in a source file

void InitMyFunctions() {
    pSomeFunction = SomeFunction;
}

Six lines of code to achieve a single function call with an extra Init routine to boot. Seriously though, who wants to work that hard?

boost::bind might find some use here but I doubt it would simplify things much.

share|improve this answer
    
+1 for another method that I hadn't thought of, but yeah, not necessarily better. I suppose it would be if I had a really large number of methods to call and more than a few of them collided. –  Ed S. Sep 8 '11 at 18:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.