Sheen Sheen - 29 days ago 9
C++ Question

Why does std::numeric_limits<long long>::max() fail?

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?

Answer

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.