Weird interaction between <algorithm> and <winsock.h>
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Weird interaction between <algorithm> and <winsock.h>

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,235

    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

  2. #2
    Join Date
    Oct 2008
    Posts
    1,090

    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 ...

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,235

    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

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Weird interaction between <algorithm> and <winsock.h>

    Quote Originally Posted by superbonzo View Post
    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

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,235

    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

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Weird interaction between <algorithm> and <winsock.h>

    Quote Originally Posted by John E View Post
    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

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,235

    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

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,798

    Re: Weird interaction between <algorithm> and <winsock.h>

    Quote Originally Posted by D_Drmmr View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center