5

This line of code fails to compile in VS2015 Update 3:

auto a = std::numeric_limits<long long>::max();

It cannot find the definition of max(). Why is this?

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
Sheen
  • 3,333
  • 5
  • 26
  • 46

1 Answers1

9

That max call may interfere with "evil" max preprocessor macro defined in the Windows SDK headers, that you have probably included (directly or indirectly).

An option is to prevent the preprocessor max macro to kick in, using an additional pair of parentheses:

... = (std::numeric_limits<long long>::max)();

As an additional option, you may consider #define #NOMINMAX before including Windows headers. This will prevent the definition of the aforementioned min and max preprocessor macros.

However, note that some Windows headers (like the GDI+ ones) do require the Win32's min and max preprocessor macros, so in such cases the use of an additional pair of parentheses may be a better option.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • This works. What is the syntax of this additional brackets? Is it C++ standard? – Sheen Nov 08 '16 at 17:06
  • 1
    Another way would be to `#undef max` after including the windows headers. – Mark Ransom Nov 08 '16 at 17:07
  • 1
    @Sheen: Yes, it's standard C++. Those additional parens prevent the preprocessor `max` macro to kick in, which sounds like the cause of your error. – Mr.C64 Nov 08 '16 at 17:07
  • 2
    By far the best option is to `#define NOMINMAX` before incuding any Windows-related headers, or even add the define to your compiler command line. This is the documented way of preventing the Windows headers from defining `min` and `max`. – Angew is no longer proud of SO Nov 08 '16 at 17:14