18

How would I go about actually calling std::max? The code won't compile in visual studio 2013, because it takes "max" as the macro.

std::max( ... );

Expects an identifier after the "std::".

Ben
  • 1,816
  • 1
  • 20
  • 29

1 Answers1

22

You can undefine the macro:

#undef max

Edit: It seems the macros can be safely disabled by putting

#define NOMINMAX

before the header files that define them (most likely windef.h, which you probably include indirectly).

To avoid undefining the macro, you can simply wrap the function in parenthesis

(std::max)(...)

As noted by chris in the comments, function-like macros require the token after the macro name to be a left parenthesis in order to expand. Wrapping the name in parentheses is really just a hack to make the next token a right parenthesis without changing the meaning once you put macros aside.

user276648
  • 6,018
  • 6
  • 60
  • 86
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Couldn't that break things that rely on the max macro? – Ben Mar 30 '14 at 12:59
  • @Ben Probably. I found a duplicate, have a look at that. You may need some kind of guards around this. I do not use VS so don't know the effects of simply undefing. – juanchopanza Mar 30 '14 at 13:01
  • Okay, I defined NOMINMAX. Works perfectly. Thanks. – Ben Mar 30 '14 at 13:07
  • 17
    @Ben, You could also do `(std::max)(...)` to avoid undefining it. – chris Mar 30 '14 at 13:14
  • @chris This is a great tip, but why does it work? – aquirdturtle Jul 17 '19 at 18:20
  • 1
    @aquirdturtle, Function-like macros require the token after the macro name to be a left parenthesis in order to expand. Wrapping the name in parentheses is really just a hack to make the next token a right parenthesis without changing the meaning once you put macros aside. – chris Jul 17 '19 at 19:41