开发者

Does Visual Studio have a namespace-sensitive macro-substitution option?

What I'm talking about is to find a way to avoid the macros in <windows.h> from polluting whatever project I'm writing.

Excerpts from windows.h:

#ifdef UNICODE
#define LoadImage  LoadImageW
#else
#define LoadImage  LoadImageA
#endif // !UNICODE

#ifdef UNICODE
#define GetMessage  GetMessageW
#else
#define GetMessage  GetMessageA
#endif // !UNICODE

The majority of macros (over 99%) I'm okay with, but some of them I just couldn't find a way to avoid.

My idea is that since I always qualify the functions calls in my particular framework, e.g. ImageTool::LoadImage, Visual Studio should have enough clue that I'm not referencing the Windows API, which are all in the root namespace, i.e. ::LoadImage. But the MACRO system does not seem to be that smart.

Is there a compiler or开发者_运维问答 preprocessor option that will just enable that?


Macro substitution are basically simple textual replacements, done before the proper compiler even starts to parse the code. Therefore they are not aware of namespaces or any other parts of the C++ syntax above the pure lexical level.

The straight forward way to avoid replacement of your identifiers is to remove the macros:

#ifdef LoadImage
#undef LoadImage
#endif

This of course will also stop following code from accessing the Windows API with the name LoadImage.


You will to live with it - you cannot avoid these macros on Windows platform. There is no macro-name-spacing in C/C++ pre-processor world. You may however, have all of your code defined and implemented before including any windows header - but that's wouldn't be possible, I believe.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜