Thread: Macro in windef.h conflicting with standard library

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Macro in windef.h conflicting with standard library

    Hey people, I'm running into some problems with the MSVC 2005 Beta compiler. This code:
    Code:
    float nearest = std::numeric_limits<float>::max();
    Gives me an error:
    "not enough actual parameters for macro 'max'"

    Using the "go to definition" feature, it zooms me over to windefs.h, in which apparently MS did this:
    Code:
    #define max(a,b) (((a)>(b))?(a):(b))


    Convenient for them, doubtless, but now I can't use std::numeric_limits<float>::max() (or for any other type).


    So what I'd like to know is, is this standard behavior? Doesn't seem like it to me, but as I don't have a copy of the standard, I can't be sure. And, regardless, does anyone know of a neat/simple workaround I can do? i.e. Will undefining the macro after including <windows.h> have any horrible side effects?

    **EDIT**
    Stupid WYSIWYG editor. For some reason, it keeps sticking that "Convenient for them" line into the code block.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Will undefining the macro after including <windows.h> have any horrible side effects?

    no, that would be fine. of course, MS uses a lot of macros that look like functions, so if you happen to use one of them that uses the max() macro itself, you could run into more compilation errors.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    There is something you can define before including windows.h - NO_DEFINE_MIN_MAX or something like that, I did it the other day but don't have the code in front of me and don't remember the exact symbol. That way you don't have to change the windows headers. Look at MSDN or in windows.h to find it.

    I wouldn't think it is standard behavior to have a macro that duplicates the name of a standard function, but that is probably why they have the symbol to remove it.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think you can also avoid invoking the macro by having a space in front of the parentheses:
    float nearest = std::numeric_limits<float>::max ();
    Or at least by wrapping the callable construct in parentheses:
    float nearest = (std::numeric_limits<float>::max)();
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I believe CornedBee is correct. This should work:
    Code:
    float nearest = (std::numeric_limits<float>::max)();
    That's precisely why the numeric_limits template defines min() and max() the way it does.
    Code:
    static _Ty (__cdecl min)();
    static _Ty (__cdecl max)();

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Wow, thanks everyone

    >>There is something you can define before including windows.h - NO_DEFINE_MIN_MAX or something like that
    Heh, oops..
    Code:
    #ifndef NOMINMAX
    
    #ifndef max
    #define max(a,b)            (((a) > (b)) ? (a) : (b))
    #endif
    etc.
    >>float nearest = (std::numeric_limits<float>::max)();
    That worked too

    @Lucky:
    I just ran a quick test, this compiles fine:
    Code:
    int asdf()
    {
    	return 5;
    }
    #define asdf(a,b) (a+b)
    
    int main()
    {
       (asdf)();
       return 0;
    }
    It also works if I define asdf as a const int and "cout << asdf". So I'm not sure what you mean..
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I just ran a quick test, this compiles fine:
    So reverse the order of function and macro. The thing is, <limits> and <windows.h> might be included in any order.

    It also works if I define asdf as a const int and "cout << asdf".
    That's because there are no parentheses. Thus, the functional-style macro is not considered.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>So reverse the order of function and macro.
    Oh, I get it. I thought the point was so that when calling the function there wouldn't be a problem. Never occurred to me that there might be problems defining it
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Hunter2
    @Lucky:
    I just ran a quick test, this compiles fine:
    It also works if I define asdf as a const int and "cout << asdf". So I'm not sure what you mean..
    Now I'm confused! You said you ran a quick test and it compiles fine, but then you say you're not sure what I mean... Could you clarify?

    [edit]Guess I'm a bit slow on the draw. Reading CornedBee's response and your follow-up, it appears we're all through here.[/edit]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The standard C Library API
    By boyfarrell in forum C Programming
    Replies: 4
    Last Post: 10-11-2008, 02:05 PM
  2. Links to learn standard C library functions...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 02-01-2002, 12:41 AM
  3. Source code of C standard library...
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 01-26-2002, 06:35 AM
  4. Source code of the standard library functions...
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-21-2002, 12:35 PM
  5. C standard library
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 12:08 PM