32

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.

Community
  • 1
  • 1
msh
  • 457
  • 1
  • 5
  • 10
  • 9
    Why doesn't the parenthesis trick work for you? Remember to wrap it around the whole expression, as in `(std::numeric_limits::min)()` – Johannes Schaub - litb Sep 08 '09 at 14:51
  • This worked. Thank you. Please sent it as a answer, I would like to accept it. – msh Sep 09 '09 at 05:41
  • Thanks a bunch, can't believe I've been murking about with #undef's for years before someone brought this up... – Roel Dec 28 '09 at 17:07
  • @Johannes Schaub - litb: Please send your comment as an answer, I would like to accept it. – msh Apr 20 '11 at 07:09
  • @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. – sergiol Feb 20 '20 at 14:23

5 Answers5

50

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!

Luis Kabongo
  • 382
  • 2
  • 11
Kiril
  • 39,672
  • 31
  • 167
  • 226
  • 2
    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. – Apeiron Aug 07 '14 at 17:46
29

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.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 1
    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. – T.E.D. Sep 08 '09 at 14:07
  • 8
    Well, it's a typical Microsoft solution... "Our macros are causing trouble? Well, we'll just add a macro to disable them!" ;) – jalf Sep 08 '09 at 15:08
  • 1
    @jalf 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.) – moswald Sep 08 '09 at 15:17
  • 4
    @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. – jalf Sep 08 '09 at 15:50
  • 1
    Replacing macro `min` in windows.h with std::min will make Windows work faster ;) `min` macro computes minimum value twice. Same for `max` macro. – Kirill V. Lyadvinsky Sep 08 '09 at 15:53
  • Thank you for your kind concern, however I would like to make it without undefining the macros. litb's solution is worked. – msh Sep 09 '09 at 05:42
  • This doesn't undefine the macro. It tells Windows not to define it in the first place. :) – jalf Dec 03 '09 at 12:50
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.

sbk
  • 9,212
  • 4
  • 32
  • 40
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.

Dewfy
  • 23,277
  • 13
  • 73
  • 121
  • 4
    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. – James McNellis Feb 16 '11 at 05:22
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)()

eniac
  • 63
  • 1
  • 7