宏定义冲突

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

猜你喜欢

转载自blog.51cto.com/yebaoshan/2108108