Boost logo

Boost :

From: Edward Diener (eddielee_at_[hidden])
Date: 2004-02-28 18:00:25

Eric Niebler wrote:
> This concerns all boost developers. Please read!
> I have just added documentation for how to deal with min/max in boost
> code. I have added it to the "Boost Library Requirements and
> Guidelines" document (boost/more/lib_guide.htm). That seemed the
> appropriate place, but it might be hard to find. If anyone thinks
> this belongs in a more prominent place, please suggest one.

I want to thank Eric for doing all the work, and for documenting it, in
order to solve this problem.

I would like to think that compiler vendors, most notably Microsoft, may
have learned not to define C/C++ macros with lowercase letters. I am aware
of macros like MessageBox to MessageBoxA or MessageBoxW depending if UNICODE
is defined or not in the windows.h header files, but even in these cases
inline function declarations forwarding the parameters to the correct
function would have solved the problem much better. Of course I am also
aware that .NET puts everything in namepaces and classes, and that this is
the future as far as Microsoft is concerned for Windows programming.

I am hoping that ideas, and perhaps implementations for all I know, which
have been presented to the C++ committee for namespace-like macros will make
such a problem obsolete in the future, and we can all look back and laugh
that such a problem as the min/max one with windows.h header file actually
needed to be solved in the days of 2004 C++ programming, much as we do now
that a time actually existed where programmers had to worry about a 640K
limit on IBM-like PCs.

Nonetheless, thanks for all your work on this.

> For reference, here is the text I have added:
> ----
> Make sure your code compiles in the presence of the min() and max()
> macros. Some platform headers define min() and max() macros which
> cause some common C++ constructs to fail to compile. Some simple
> tricks can protect your code from inappropriate macro substitution:
> * If you want to call std::min() or std::max():
> o Use (std::min)(a,b) if you do not require
> argument-dependent
> look-up.
> o Use boost::std_min(a,b) if you do require
> argument-dependent look-up. boost::std_min() delegates
> to std::min().
> * If you want to call std::numeric_limits<int>::max(), use
> (std::numeric_limits<int>::max)() instead.
> * If you want to call a min() or max() member function, instead
> to doing obj.min(), use (obj.min)().
> * If you want to declare or define a function or a member
> function named min or max, then you must use the
> BOOST_PREVENT_MACRO_SUBSTITUTION macro. Instead of writing int
> min() { return 0; } you should write int min
> BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } This is true
> regardless if the function is a free (namespace scope)
> function, a member function or a static member function, and
> it applies for the function declaration as well as the
> function definition.

Boost list run by bdawes at, gregod at, cpdaniel at, john at