Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

min / max macros vs. std::min, and std::max

1,339 views
Skip to first unread message

Steven S. Wolf

unread,
Dec 4, 2002, 4:22:30 PM12/4/02
to
(Visual C++ 6 sp5)

I dunno about any of you, but I am forever running into frustrating
cricumstances where WINDOWS.H (and it's ilk) refuse to compile because
#define min/max/MIN/MAX have been undefined, or vice versa, where I need to
use some part of the standard library and min/max/MIN/MAX muck up the
template definitions, e.g.:

class c
{
public:
c() : m_max(std::numeric_limits<double>::max()) { }
}

compiler complains that there are insufficient formal parameters for macro
max.

If I insert:

#undef max

Before my class declaration, then some other file in my build says something
stupid like error C2065: 'max' ; undeclared itentifier.

--

What in blazes is a workable method to both use std::min and std::max keep
stupid windows.h and it's ilk compiling properly?!@#$!@#$

Thanks,

Steven S. Wolf


Craig Powers

unread,
Dec 4, 2002, 5:03:18 PM12/4/02
to

You can use _cpp_min and _cpp_max (or _MIN and _MAX), which are the
names given to std::min and std::max to avoid the collision.


Alternatively, you could do something like this:

prior to inclusion of <windows.h>,
#define NOMINMAX

prior to inclusion of whichever file it is min and max are supposed
to be in (they're actually in <xutility> which gets included by
several others),
#define _cpp_min min
#define _cpp_max max

and cross your fingers that nothing makes the mistake of using
windows::min and windows::max. If it's in your own code, it ought
to be possible to rewrite it to use std::min or std::max.

It's a bit of a hack; I use it because I have no use for the Windows
macros and I don't particularly care for the idea of using _cpp_min.

--
Craig Powers
MVP - Visual C++

Craig Powers

unread,
Dec 4, 2002, 5:07:12 PM12/4/02
to
Craig Powers wrote:

>
> "Steven S. Wolf" wrote:
> >
> > c() : m_max(std::numeric_limits<double>::max()) { }
>
> You can use _cpp_min and _cpp_max (or _MIN and _MAX), which are the
> names given to std::min and std::max to avoid the collision.

Which has no equivalent for std::numeric_limits. Duh. (IMO, an
oversight in the library.)

Which makes this:


> prior to inclusion of <windows.h>,
> #define NOMINMAX
>

> and cross your fingers that nothing makes the mistake of using
> windows::min and windows::max. If it's in your own code, it ought
> to be possible to rewrite it to use std::min or std::max.

the only option.

Arnaud Debaene

unread,
Dec 4, 2002, 6:17:04 PM12/4/02
to
Steven S. Wolf wrote:
> (Visual C++ 6 sp5)
>
> I dunno about any of you, but I am forever running into frustrating
> cricumstances where WINDOWS.H (and it's ilk) refuse to compile because
> #define min/max/MIN/MAX have been undefined, or vice versa

Halas yes! I hope the responsible for so silly macros in windows.h has been
fired! Use #define NOMINMAX before #including <windows.h>. If some code
break after that (never know), define yourself locally min and max. It's
ugly but I fear it's the only way.

Arnaud
MVP - VC


Johan Rosengren

unread,
Dec 4, 2002, 11:30:01 PM12/4/02
to
<snip>

> Halas yes! I hope the responsible for so silly macros in windows.h has
been
> fired! Use #define NOMINMAX before #including <windows.h>. If some code
> break after that (never know), define yourself locally min and max. It's
> ugly but I fear it's the only way.
>

Well, the Windows.h macro predates this newfangled C++-stuff, so I think
he/she is excused :-)

Johan Rosengren
Abstrakt Mekanik AB

> Arnaud
> MVP - VC
>
>


James Curran/MVP

unread,
Dec 5, 2002, 9:27:19 AM12/5/02
to
"Johan Rosengren" <johan.r...@telia.com> wrote in message
news:#2XH9mBnCHA.456@TK2MSFTNGP11...

> Well, the Windows.h macro predates this newfangled C++-stuff, so I think
> he/she is excused :-)

No, they're not, because a) they should have been MIN() and MAX() instead of
min() & max() to avoid conflict with functions, and b) general purpose
functions like min & max shouldn't be in a specific purpose header like
windows.h anyway.


Johan Rosengren

unread,
Dec 5, 2002, 10:16:26 AM12/5/02
to

"James Curran/MVP" <james...@mvps.org> wrote in message
news:eS7xppGnCHA.2412@TK2MSFTNGP09...

You're right, of course. On the other hand, a stern reprimand ought to be
enough, this long after the deed :-)

James Curran/MVP

unread,
Dec 5, 2002, 2:31:54 PM12/5/02
to
"Johan Rosengren" <johan.r...@telia.com> wrote in message
news:ejQO7KHnCHA.2012@TK2MSFTNGP11...

> You're right, of course. On the other hand, a stern reprimand ought to be
> enough, this long after the deed :-)

I still vote for slapping him.


0 new messages