CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2004
    Posts
    75

    min / max macros

    I'm just working on a project that makes heavy use of the min(a,b) and max(a,b) macros defined in Windef.h. I would really prefer using std::min(...) and std::max(...) instead, not to mention std::numeric_limits<>::min() and std::numeric_limits<>::max().

    It just hurts my head, so is there a trick to somehow bypass the macro expansion where I want it to? Or will I really have to #undef min/max and include<minmax.h> at the end of my files

    Oliver

  2. #2
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: min / max macros

    If you #define NOMINMAX before including any of the Windows headers, then those macros will not be defined. That's probably the easiest way to get rid of them.

  3. #3
    Join Date
    May 2004
    Posts
    75

    Re: min / max macros

    Yes, but as I wrote, I'm working on a project that makes heavy use of these macros. I cannot tell all developers to rewrite their code just because I globally undefine these macros.

    So I'm looking for a workaround applicable to my portion of code, only.

  4. #4
    Join Date
    Sep 2004
    Posts
    519

    Re: min / max macros

    Quote Originally Posted by Oliver M.
    Yes, but as I wrote, I'm working on a project that makes heavy use of these macros. I cannot tell all developers to rewrite their code just because I globally undefine these macros.

    So I'm looking for a workaround applicable to my portion of code, only.
    In your cpp-files between the includes and your code:
    Code:
    // include above
    #undef MAX
    #undef MIN
    // your code below
    Be certain to only put this in the cpp-files or you risk affecting everybody else.

    If you really wish to undef macros in a header file and you are using MS Visual Studio.NET 2003:

    Code:
    // include above
    #pragma push_macro("max")
    #pragma push_macro("min")
    #undef MAX
    #undef MIN
    
    // your code here
    
    #pragma pop_macro("min")
    #pragma pop_macro("max")
    // end of file below
    In conclusion I wish to applaud you for doing what I believe is the-right-thing. Macros should be avoided in most cases and shall definitely be avoided when there are superior alternatives such as std::max and std::min.

    Hope this helps

  5. #5
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    Re: min / max macros

    Quote Originally Posted by Oliver M.
    Yes, but as I wrote, I'm working on a project that makes heavy use of these macros. I cannot tell all developers to rewrite their code just because I globally undefine these macros.
    Sorry, I thought you meant that you wanted to change all instances of those macros to the corresponding functions in std. My mistake.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: min / max macros

    Is there a reason you want to go to all this trouble? If it's already written one way, it's probably best to leave it that way.

  7. #7
    Join Date
    May 2004
    Posts
    75

    Re: min / max macros

    @marten_range
    Ah, the pragma's! I'll enclose them with #ifdef _MSC_VER and off I go!
    Thank you, this is a life-safer!

    And thank you for your applause. I'm not so critical with MS code, most of the time considering that it is old and grown. The macros were useful as long as there were no templates. But this MS developer, who created the small-letter macros min/max should be lapidated for sure.

    @Smasher/Devourer
    Never mind. I really do not enjoy stepping through a big code base, so I'll avoid it as long as possible

    @GCDEF
    There are at least 3 good reasons that comes to my mind:

    1) As I pointed out in my original post, I'd really like (not to say I need) to use e.g. std::numeric_limits<>::max() and std::numeric_limits<>::min() which both do not work anymore because the *lovely* preprocessor attempts to replace the min() and max() with its ((a)<(b)?(a): (b)) what fails because of the missing arguments.

    2) The macros use operator< for min(a,b) and operator> for max(a,b).

    3) If a==b, macros min(a,b) and max(a,b) evaluate to b while std::min(a,b) and std::max(a,b) return a. So this example code - if it would compile - would assert:
    Code:
    int i(5);
    int j(5);
    assert(&min(i,j) == &std::min(i,j)); // Asserts if i==j!
    assert(&max(i,j) == &std::max(i,j));  // Asserts if i==j!

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: min / max macros

    Seems like a waste of time to me, but I guess you have your reasons.

  9. #9
    Join Date
    May 2004
    Posts
    75

    Re: min / max macros

    Quote Originally Posted by GCDEF
    Seems like a waste of time to me, but I guess you have your reasons.
    Based on your answer I assume that you did not understand the issue. Please reread my post!

    I need to use e.g. std::numeric_limits<>::max(), so how can I do so if the max(a,b) macro is defined? Or do you know an alternative to std::numeric_limits<> that I do not know? Please provide me with your solution or at least explain your "waste of time" judgement!

    The problem was sufficiently solved by the reply of marten_range.

  10. #10
    Join Date
    May 2004
    Posts
    75

    Re: min / max macros

    Occasionally found another solution for accessing the numeric_limits::min/max while peeking into the boost library code. Sometimes it's so simple after all:
    Code:
    // Will not compile if max(a,b) macro is defined:
    T my_maximum1 = std::numeric_limits<T>::max();
    
    // Compiles and works fine:
    T my_maximum2 = (std::numeric_limits<T>::max)();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured