macro and member function conflict
Asked Answered
A

5

33

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.

Antonetteantoni answered 8/9, 2009 at 13:46 Comment(5)
Why doesn't the parenthesis trick work for you? Remember to wrap it around the whole expression, as in (std::numeric_limits<T>::min)()Continually
This worked. Thank you. Please sent it as a answer, I would like to accept it.Antonetteantoni
Thanks a bunch, can't believe I've been murking about with #undef's for years before someone brought this up...Decomposed
@Johannes Schaub - litb: Please send your comment as an answer, I would like to accept it.Antonetteantoni
@JohannesSchaub-litb The upvote I've done in the accepted answer should be for you, not for someone else who made an answer exactly as your comment two years later.Makepeace
M
53

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!

Morton answered 23/8, 2011 at 19:5 Comment(1)
From what I understand it processes the value in the parenthesis first. the macro also takes in two parameters and but enclosing the function parenthesis it won't show any parameters to the macro.Showker
M
30

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.

Mopboard answered 8/9, 2009 at 14:0 Comment(7)
Voted up, as we came to the same conclusion here. Among other evils, it adds about 19K per object file. We just created our own header file with the few things we typically need from windows.h in it.Allow
Well, it's a typical Microsoft solution... "Our macros are causing trouble? Well, we'll just add a macro to disable them!" ;)Mopboard
@Mopboard Actually, windows.h was implemented a looong time ago, well before std::min (or any other standard min/max) was created. Changing windows.h by removing their min/max would have broken way too much code. Microsoft (nearly) always errs on the side of caution there: if you don't need min/max, you can tell it NOMINMAX. (This does not address the original problem where they defined a macro with non-capital letters.)Pajamas
@mos: I know, but as you point out, the root problem is that they gave the macros ridiculously bad names to begin with. And what's worse is that they then decided to create hundreds of new badly named macros when they added unicode support. They obviously hadn't learned anything from min/max.Mopboard
Replacing macro min in windows.h with std::min will make Windows work faster ;) min macro computes minimum value twice. Same for max macro.Smoothspoken
Thank you for your kind concern, however I would like to make it without undefining the macros. litb's solution is worked.Antonetteantoni
This doesn't undefine the macro. It tells Windows not to define it in the first place. :)Mopboard
F
2

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.

Fernandez answered 8/9, 2009 at 14:34 Comment(1)
Thank you. However in my project, that make much more headache.Antonetteantoni
T
1

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.

Tsimshian answered 8/9, 2009 at 13:48 Comment(1)
Note that the #ifdef is unnecessary. It is ok to #undef a name that isn't defined as a macro, so #undef min is safe, regardless of whether min is defined.Wellappointed
S
1

Dewfy, The problem with that solution is if you nee to use the macro afteryards.

I even tried the defining NOMINMAX but it didn't work.

The best solution i found was the one from Johannes Schaub: (std::numeric_limits::min)()

Silma answered 4/2, 2010 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.