Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

macro and member function conflict

Tags:

c++

I have problem that,std::numeric_limits::min() conflicts with the "min" macro defined in "windef.h". Is there any way to resolve this conflict without undefine the "min" macro. The link below gives some hints, however I couldn't manage to use parenthesis with a static member function.

What are some tricks I can use with macros?

Thank you in advance.

like image 813
msh Avatar asked Sep 08 '09 13:09

msh


4 Answers

The workaround is to use the parenthesis: int max = (std::numeric_limits<int>::max)();

It allows you to include the windef.h, doesn't require you to #undef max (which may have adverse side effects) and there is no need to #define NOMINMAX. Works like a charm!

like image 192
Kiril Avatar answered Nov 10 '22 17:11

Kiril


The only really general solution is to not include windows.h in your headers.

That header is a killer, and does pretty much anything it can to make your code blow up. It won't compile without MSVC language extensions enabled, and it is the worst example of macro abuse I've ever seen.

Include it in a single .cpp file, and then expose wrappers in a header, which the rest of your code can use. If windows.h isn't visible, it can't conflict with your names.

For the min/max case specifically, you can #define NOMINMAX before including windows.h. It will then not define those specific macros.

like image 25
jalf Avatar answered Nov 10 '22 17:11

jalf


In addition to jalf's answer, you could also #define WINDOWS_LEAN_AND_MEAN before including windows.h. It will get rid off min, max and some more noise from windows headers.

like image 2
sbk Avatar answered Nov 10 '22 15:11

sbk


Yep, I've meet the same problem. I found only one solution:

#ifdef min
#undef min
#endif //min

Place it right after includes have done.

like image 1
Dewfy Avatar answered Nov 10 '22 16:11

Dewfy