Thread: Is undefining "max" the only way to call numeric_limits<double>::max() in VS?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    Is undefining "max" the only way to call numeric_limits<double>::max() in VS?

    Hi,

    For this fragment:
    Code:
    std::numeric_limits<double>::max();
    MSVC gave me the error:

    1>.\dbqWrapper.cpp(109) : warning C4003: not enough actual parameters for macro 'max'
    1>.\dbqWrapper.cpp(109) : error C2589: '(' : illegal token on right side of '::'
    1>.\dbqWrapper.cpp(109) : error C2059: syntax error : '::'
    It took me one hour and probably one pund of my body fat to finally try

    Code:
    #undef max
    std::numeric_limits<double>::max();
    with success.
    I was not aware of the fact that the preprocessor is cabable to overwrite function names from inside the STL.
    Is the #undef max the standard way of dealing with that or how do you do it?
    Thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That should not be necessary. Try:
    Code:
    #include <limits>
    #include <iostream>
    
    int main()
    {
        std::cout << std::numeric_limits<double>::max() << std::endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    You are right, I tried it 2 seconds ago. Seems I've included something which pulls the max macro.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You may want to #define NOMINMAX or NO_MIN_MAX (I always forget which) anyway, since som e innocuous change to the code could bring the error back. I believe NOMINMAX is Microsoft's suggested solution for this bug. You can confirm this by searching for that text on MSDN.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    519

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The culprit is <windows.h>, by the way. I have no idea what possessed the Win32 engineers to define these macros back then, but now backward compatibility prevents them from removing them.
    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

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    The culprit is <windows.h>, by the way. I have no idea what possessed the Win32 engineers to define these macros back then, but now backward compatibility prevents them from removing them.
    Why couldn't they just change it so that you need to define a pre-processor symbol if you want those macros, otherwise they aren't included?

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by cpjust View Post
    Why couldn't they just change it so that you need to define a pre-processor symbol if you want those macros, otherwise they aren't included?
    You mean the other way around, surely. That way old code would break.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Mario F. View Post
    You mean the other way around, surely. That way old code would break.
    Only if the old code actually used those macros. It sure wouldn't be the first time MS deprecated old "features" in their compiler. Besides, you need to draw the line at some point and say enough is enough, get rid of the useless crap.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by cpjust View Post
    Besides, you need to draw the line at some point and say enough is enough, get rid of the useless crap.
    Whatever you do it will damage something or force new code to new unexpected rules. It's not such a big problem really that deserves a treatment. If you look at it on the bright side, it is left there as a reminder to us all that not following the guidelines, as we often preach but don't actually do, can have that kind of outcome.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by pheres View Post
    It took me one hour and probably one pund of my body fat to finally try
    The error message had the word "macro" right in it...

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    From the MSDN link:
    STATUS

    This behavior is by design.
    I think they meant:
    STATUS

    This behavior is due to bad design.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 02:40 AM
  2. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 06:07 AM
  3. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM