轉自:http://hi.baidu.com/solidmcp/blog/item/3e8fd789d4142ebd0f2444f2.html 。介紹的方法不錯,可以解決很多宏衝突的問題。
奇技淫巧之避免因宏帶來的名字衝突
>> 退避三舍:
既成事實,不得不低頭,你把max的名兒佔了,那我就叫iMax;
>> 刮骨療傷:
中毒沒問題,格老子效仿關雲長刮骨療傷。
看看人家POCO怎麼做的,你"Bill大門們" 不是喜歡在API後面又A又W的,不高興,直接把你們給UN掉。
//------------------------------------------------------------------------------------
#include <windows.h>
#if !defined(POCO_NO_UNWINDOWS)
// A list of annoying macros to #undef.
// Feel free to extend as required.
#undef CreateFile
#undef DeleteFile
[][][][][][] 此處省略數行,[都廢]了
#undef GetObject
#endif // POCO_NO_UNWINDOWS
//------------------------------------------------------------------------------
>> 以毒攻毒:
你有唐門暗器,我有吸星大法,你朝我射,老子抱死你。
Boost內有一個很經典的用法,爲的就是max和min在window已被定義。
// Workaround for names overriden by defined Macros
// One known example is max/min:
// In windows, max/min are defined if you include <windows.h>
// so, you will encounter problem while using std::max or std::min if you include it before std's <algorithm>
// In Boost, there is a workaround by adding an empty Macro BOOST_PREVENT_MACRO_SUBSTITUTION after max or min,
// so that max and min are not recognized as the ones defined by macors, but std::max/min instead.
// Just borrow it from boost.
//
#define SMCP_WORKAROUND_NAME_OVERRIDE
// How to use:
double GetMaxValue()
{
double dA = 20.0;
double dB = 30.0;
return max SMCP_WORKAROUND_NAME_OVERRIDE(dA, dB);
}
>> 暗渡陳倉:
以上幾招,Piaoger常用,屬於已知範疇,唯獨這“暗渡陳倉”,Piaoger未嘗聞也。
暗渡陳倉,其實就是在函數名給戴個TT:
// WinBase.h 有#define Yield()
#include <windows.h>
class Thread
{
public:
Thread();
void (Yield)() {};
};
對了, 構造函數也可以的哦。
包裹住函數名TT抑制了同名的函數形式的宏的展開,果然安全啊。
對了,那怎麼用呢:
Thread my_thread;
(my_thread.Yeild)()
少了些快意,多了些醜陋,但安全第一。