Using a namespace to avoid name collisions

I was creating one of the projects I am working with, this time with VS2010, just to find out that one of the inclusions in windows.h has a typedef INPUT that ran into the const const export line in the code that I already have.

//winuser.h(string: 5332)

typedef struct tagINPUT {
    DWORD   type;

    union
    {
        MOUSEINPUT      mi;
        KEYBDINPUT      ki;
        HARDWAREINPUT   hi;
    };
} INPUT, *PINPUT, FAR* LPINPUT;

//foo.h

//stuff here
extern FOO_DLL_API const string INPUT;

Now I do not use INPUT in violation of .cpp (and I do not own most of the code), and trying to minimize the impact, I did the following:

//myfile.cpp

#include <foo.h>
namespace windowsLib {    //I added this
#  include <windows.h>
}

using namespace windowsLib;

This approach is working fine so far, but I wanted to ask you if you see potential problems with this approach or if you have a better suggestion.

Edit:

, . , , foo.h . , , , .

" ", ?

, , - .. , .

2:

, :

//stub.cpp

#include <windows.h>

//Implementation of wrapper methods

//stub.h

class stub {
    //public wrapper methods
}

//myfile.cpp

#include <stub.h>
#include <foo.h>    

, , . .

+3
4

, . , :

//myfile.cpp

#define INPUT UnusedSymbol
#include "foo.h"
#undef INPUT

#include "windows.h"

, INPUT foo.h, , , myfile.cpp. UnusedSymbol - , , .

+1

( , ), , <windows.h>, , windowsLib.

!

+3

, .

1) , , using namespace windowsLib; . INPUT ?

2) , windows.h . , , , INPUT. ?

.

+2

windows.h . , . , .

foo.h:

namespace fooLib {
#include "foo.h"
}

using fooLib;

, foo, .

foo, foo foo globals -. , windows.h. , .

, foo.h fooLib foo.h.

If you can touch foo.h, it's best to rename INPUT to foo.h or put the stuff foo.h in your own namespace. I think the namespace fooLibwould have a big (obvious) benefit.

+1
source

All Articles