本文共 1651 字,大约阅读时间需要 5 分钟。
转自: 。介绍的方法不错,可以解决很多宏冲突的问题。
奇技淫巧之避免因宏带来的名字冲突
>> 退避三舍:
既成事实,不得不低头,你把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// so, you will encounter problem while using std::max or std::min if you include it before std's // 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() #includeclass Thread { public: Thread(); void (Yield)() {}; };
对了, 构造函数也可以的哦。包裹住函数名TT抑制了同名的函数形式的宏的展开,果然安全啊。对了,那怎么用呢:
Thread my_thread; (my_thread.Yeild)()
少了些快意,多了些丑陋,但安全第一。
转载地址:http://eliob.baihongyu.com/