Weird interaction between <algorithm> and <winsock.h>
Code:
#include <algorithm>
#include <winsock.h>
void some_func()
{
int result = std::min (0, 16384);
}
Okay, it's a silly example - but the above code consistently gives me error C2589: '(' : illegal token on right side of '::'. I'm building with VC8. If I don't #include <winsock.h> the code compiles correctly but it doesn't matter if I #include <winsock.h> before or after including <algorithm>
I guess it might be due to one of my #defines but I've tried the code in a very minimal program and I still get the error. Is there anyone here who could try this (preferably in VC8) and let me know if they also see the problem?
Re: Weird interaction between <algorithm> and <winsock.h>
unfortunately, Microsoft headers are polluted with their own min/max macros that conflict with the corresponding stl function templates. Just define NOMINMAX before any windows related inclusion to suppress those macro definitions ...
Re: Weird interaction between <algorithm> and <winsock.h>
Thanks for the fast response Superbonzo. You were absolutely right.
I'm amazed that I've been programming for so many years but I never encountered this problem until today! :eek:
Re: Weird interaction between <algorithm> and <winsock.h>
Quote:
Originally Posted by
superbonzo
Just define NOMINMAX before any windows related inclusion to suppress those macro definitions ...
Just to add, an alternative for when you cannot (or don't want to) define NOMINMAX is to write
Code:
int result = (std::min)(0, 16384);
Re: Weird interaction between <algorithm> and <winsock.h>
Thanks D_Drmmr, that solution works too - although it's left me a bit confused. Why is (std::min) considered as being different from std::min :confused:
Re: Weird interaction between <algorithm> and <winsock.h>
Quote:
Originally Posted by
John E
Thanks D_Drmmr, that solution works too - although it's left me a bit confused. Why is (std::min) considered as being different from std::min :confused:
Because it cannot be interpreted as a macro call, so the preprocessor just leaves it alone. With the code in your OP, the preprocessor will make it into something like this
Code:
int result = std::(((0) < (16384)) ? (0) : (16384));
Re: Weird interaction between <algorithm> and <winsock.h>
Ah yes, obvious now you've explained it. Thanks! :thumb:
Re: Weird interaction between <algorithm> and <winsock.h>
Quote:
Originally Posted by
D_Drmmr
Because it cannot be interpreted as a macro call, so the preprocessor just leaves it alone.
to enlighten this...
the macro you're stumbling over is a define with parameters (even if the number of parameters is zero)
#define min(x,y) somethingsomething
// or even #define min() somethingsomething with zero parameters
this is different from a macro defined without parameters
#define min somethingsomething
in the std::min(0,16384) case, the macro 'min()' is a match. This will potentially be causing a different amount of arguments error C4002 or C4003, but in this case std::min has 2 parameters also. Note that macro matching logic DOES NOT depend on the number of parameters like in trying to match a function which can have overloads on different types and number of parameters (macro's are unique, you can't overload different number of parameters).
by writing (std::min)(something) the parens around std::min exclude the possibility for the macro 'min()' to match. But it would allow a macro 'min' to match.
The "trick" here works because min is defined as a macro with parameters.