-
July 24th, 2013, 02:47 AM
#1
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?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 24th, 2013, 03:00 AM
#2
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 ...
-
July 24th, 2013, 03:10 AM
#3
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!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 24th, 2013, 03:58 AM
#4
Re: Weird interaction between <algorithm> and <winsock.h>
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);
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 24th, 2013, 04:28 AM
#5
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
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 24th, 2013, 04:37 AM
#6
Re: Weird interaction between <algorithm> and <winsock.h>
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
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));
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 24th, 2013, 06:01 AM
#7
Re: Weird interaction between <algorithm> and <winsock.h>
Ah yes, obvious now you've explained it. Thanks!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
July 24th, 2013, 09:19 AM
#8
Re: Weird interaction between <algorithm> and <winsock.h>
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|