C++中的宏和函数名称的冲突 STL+windows.h
2009-03-03 12:21
363 查看
最近在写代码的时候总是发现在#include"windows.h"了以后经常用STL中的min.max函数出现编译错误。但是自认为代码是没有错误的。下面就是相关的代码:
这里的numeric_limits模板类和那个std::max都不约而同的遇上了编译错误:
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2589: '(' : illegal token on right side of '::'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2059: syntax error : '::'
搜索了网上看到了这个帖子.确实冲突是存在的。在#include"windows.h"之后,编译器把这里的max()调用都用windef.h里面的宏展开了.
说明宏的替换是发生在语法分析之前的。当然解决方案也是有的。最简单的方案就是在工程编译的时候添加一个NOMINMAX的宏,那样这些宏就不会编译在里面,那问题也解决了。还有一个办法就是你在调用函数的时候都加上括号,让函数看上去想函数指针一样:
虽然这样的话,问题解决了。但是留给我们更多的想法是函数名称定义上需要注意的地方。如果我自己定义一个类,然后里面的函数名是max或者min的话,是不是在调用的时候也有一样的问题呢??回答是肯定的。还是前面的例子。
尝试加入一个Test类,里面有一个max函数。这里不需要调用就不能通过编译,因为编译器已经给你做了展开。
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : warning C4003: not enough actual parameters for macro 'max'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2059: syntax error : ')'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
这个就是相关的错误。给我们的提醒就是函数名称的定义不要和宏定义的名称相同,或者说在C++里面尽量不要用宏!
// testMacro.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <limits> #include <algorithm> #include "windows.h" int _tmain(int argc, _TCHAR* argv[]) { int k = std::numeric_limits<int>::max(); int b = std::max(1,3); return 0; }
这里的numeric_limits模板类和那个std::max都不约而同的遇上了编译错误:
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2589: '(' : illegal token on right side of '::'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2059: syntax error : '::'
搜索了网上看到了这个帖子.确实冲突是存在的。在#include"windows.h"之后,编译器把这里的max()调用都用windef.h里面的宏展开了.
#ifndef NOMINMAX #ifndef max #define max(a,b) (((a) > (b)) ? (a) : (b)) #endif #ifndef min #define min(a,b) (((a) < (b)) ? (a) : (b)) #endif #endif /* NOMINMAX */
说明宏的替换是发生在语法分析之前的。当然解决方案也是有的。最简单的方案就是在工程编译的时候添加一个NOMINMAX的宏,那样这些宏就不会编译在里面,那问题也解决了。还有一个办法就是你在调用函数的时候都加上括号,让函数看上去想函数指针一样:
int k = (std::numeric_limits<int>::max)(); int b = (std::max)(1,3);
虽然这样的话,问题解决了。但是留给我们更多的想法是函数名称定义上需要注意的地方。如果我自己定义一个类,然后里面的函数名是max或者min的话,是不是在调用的时候也有一样的问题呢??回答是肯定的。还是前面的例子。
#include "stdafx.h" #include <limits> #include <algorithm> #include "windows.h" class Test { public: int max() { return 1; } }; int _tmain(int argc, _TCHAR* argv[]) { int k = (std::numeric_limits<int>::max)(); int b = (std::max)(1,3); return 0; }
尝试加入一个Test类,里面有一个max函数。这里不需要调用就不能通过编译,因为编译器已经给你做了展开。
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : warning C4003: not enough actual parameters for macro 'max'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2059: syntax error : ')'
1>c:/project/testapplication/c++/testmacro/testmacro/testmacro.cpp(12) : error C2334: unexpected token(s) preceding ':'; skipping apparent function body
这个就是相关的错误。给我们的提醒就是函数名称的定义不要和宏定义的名称相同,或者说在C++里面尽量不要用宏!
相关文章推荐
- STL运用的C++技术(4)---函数重载
- C/C++/动态链接库DLL中函数的调用约定与名称修饰
- C++STL之函数对象及谓词
- linux和windows下,C/C++开发的延时函数,sleep函数
- c++中STL怎样编写自己的函数对象
- C++STL之函数对象及谓词
- [Windows]_[C/C++]_[WIN32根据程序名(映像名称)终止外部程序]
- C++中使用sigaction结构体名和函数名冲突的问题
- C++ STL copy函数效率分析
- C++ STL list 成员函数 sort算法分析
- C++ STL/ (12) 函数对象适配器
- 【转载】:【C++跨平台系列】解决STL的max()与numeric_limits::max()和VC6 min/max 宏冲突问题
- C++STL 常用 函数 用法
- C++中的函数名称粉碎机制和它的逆向应用
- STL运用的C++技术(6)——函数对象 .
- C++ Standard Stl -- SGI STL源码学习笔记(07) stl_vector 与 一些问题的细化 3 resize函数剖析
- [C++再学习系列] 函数声明与STL容器构造
- c++ stl 函数作为演算法参数
- 从零开始学C++之STL(八):函数对象、 函数对象与容器、函数对象与算法
- c++ 提高4 map容器 共性机制 使用时机 比较| STL算法 算法基础仿函数 谓词 函数适配器 遍历算法