宏定義衝突

1、stl 中max min衝突

宏是沒有命名空間的,其在預編譯階段完成,而命名空間在編譯階段

(1) 如下代碼,將會編譯出錯

namespace TT

{

#define MAX(a,b)            (((a) > (b)) ? (a) : (b))

}

namespace Test

{

typedef void (*Func)();

int MAX(int a, Func func)

//int MAX(int a, int b)

{

return 0;

}

}

(1)如下test.cpp文件內容,編譯不能通過

#define max(a,b)            (((a) > (b)) ? (a) : (b))

namespace TEST{

inline int max(int a, int b)

{

return            (((a) > (b)) ? (a) : (b));

}

}

(2)如下test.cpp文件內容,編譯將報錯,因爲algorithm中包含max(const _Tp&, const _Tp&);

#include <list>

#define max(a,b)            (((a) > (b)) ? (a) : (b))

#include <algorithm>

(3)但比較奇怪的是,如下內容,卻能編譯通過

#define max(a,b)            (((a) > (b)) ? (a) : (b))

#include <algorithm>

(4) 關於(2)和(3)的原因.通過跟蹤stl代碼,發現如下

  • stl源碼中存在頭文件:c++/config.h 在/usr/include/c++/4.8/x86_64-w64-mingw32/bits/ (64位)或/usr/include/c++/4.8/i686-w64-mingw32/bits/ (32位) .該頭文件有包含如下部分

#ifndef _GLIBCXX_CXX_CONFIG_H

#define _GLIBCXX_CXX_CONFIG_H 1

......

// For example, <windows.h> is known to #define min and max as macros...

#undef min

#undef max

......

#endif // _GLIBCXX_CXX_CONFIG_H

  • 一般<list>或者<algorithm>等頭文件,都包含c++config.h頭文件.

  • 預編譯時, 對應(2) 當#include<list>,將包含進c++ config.h頭文件,引入了#undef min 和#undef max  接下來在#define max 導致又有了max的宏定義,最後#include<algorithm>時,雖然它也包含c++config.h頭文件,但是由於c++config.h有哨衛保護,即如上的_GLIBCXX_CXX_CONFIG_H,其在#include<list>時已經被引入包含了,所以在#include<algorithm>時,導致並不會再包含進此頭文件,所以不會再引入#undef max. 最終導致此後#include<algorithm>中的std::max()被定義的宏替換了,導致此後的編譯失敗

  • 跟蹤方法,逐步跟蹤各個頭文件,加註釋,再預編譯看結果g++ -E test.cpp -o test.i

#include <list>

#define max(a, b) ((a) > (b) ? (a) : (b))

//#include "c++config.h"

//void max(int a, int b);

//{}

//#include <algorithm>

//#include <vector>

#include </usr/include/c++/4.8/bits/algorithmfwd.h>

//void max(int a, int b, int c);

//{}

int main()

{

return 0;

}

2.#與##的使用

     (1) #就是將後面的 宏參數 進行字符串操作,就是將後面的參數用雙引號引起來.

##就是用於連接。

比如:

#define PRINT(NAME) printf("token"#NAME"=%d\n", token##NAME)

調用時候使用: PRINT(9);

宏展開即爲: printf("token"#9"=%d\n",token##9);

#9即爲"9",token##9即爲: token9

整個爲: printf("token""9""=%d\n",token9);

之前定義過token9爲9,所以就是輸出 token9=9;

(2) gcc下不允許##後面出現符號,比如+-*/.->之類的,否則編譯不通過

  However, two tokens that don’t together form a valid token cannot be pasted together. For example, you cannot concatenate x with + in either order. If you try, the preprocessor issues a warning and emits the two tokens. Whether it puts white space between the tokens is undefined

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章