Discussion:
min/max macros in C
(too old to reply)
Vladimir Grigoriev
2010-01-28 19:07:08 UTC
Permalink
It seems that min/max macros are not a part of the C standard. Does anybody
know the rationale of this?

Vladimir Grigoriev
Victor Bazarov
2010-01-28 19:56:32 UTC
Permalink
Post by Vladimir Grigoriev
It seems that min/max macros are not a part of the C standard. Does anybody
know the rationale of this?
I am *sure* somebody in 'comp.std.c' might know. It's even possible
that somebody in 'comp.lang.c' knows, too.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Tim Roberts
2010-01-29 05:03:55 UTC
Permalink
Post by Vladimir Grigoriev
It seems that min/max macros are not a part of the C standard. Does anybody
know the rationale of this?
Well, they're trivially easy to type in on your own, but to make them
polymorphic, you make them unsafe.

The original C89 committee focused on codifying existing practice rather
than inventing new features. Perhaps there weren't enough existing
compilers with min/max implementations.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Vladimir Grigoriev
2010-01-29 15:32:05 UTC
Permalink
Post by Tim Roberts
The original C89 committee focused on codifying existing practice rather
than inventing new features. Perhaps there weren't enough existing
compilers with min/max implementations.
It is strange enough because all old C compilers had usually min/max macros.
And it is funny that even now users using modern C compilers define min/max
macros yourself. It looks like a gap in the C standard because it does not
offer an alternative.

Vladimir Grigoriev
Ulrich Eckhardt
2010-02-01 08:37:46 UTC
Permalink
Post by Vladimir Grigoriev
Post by Tim Roberts
The original C89 committee focused on codifying existing practice rather
than inventing new features. Perhaps there weren't enough existing
compilers with min/max implementations.
It is strange enough because all old C compilers had usually min/max
macros. And it is funny that even now users using modern C compilers
define min/max macros yourself. It looks like a gap in the C
standard because it does not offer an alternative.
Some consider macros evil. I'd even consider those very macros evil because
they can break if the 'arguments' contain side effects. At least they
should be called MIN/MAX instead. Further it isn't hard to write similar
code using the ternary operator. Still, I'm aware that many people seem to
like the macro.

I guess there simply wasn't enough consent for their inclusion but strong
voices against.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Vladimir Grigoriev
2010-02-01 11:43:51 UTC
Permalink
Ulrich, your position has no any reasonable base. In fact it looks the
following way. You say that macros (as it follows including min/max macros)
are evils. O.k., and what to do to find minimal/maximum values? You
answer: "To write these macros yourself!" Is not it very strange, is it? And
what do compiler producers do? They include these macros in their libraries!
Or they advise users to write these macros themselves.:)

Vladimir Grigoriev

.
Post by Ulrich Eckhardt
Post by Vladimir Grigoriev
Post by Tim Roberts
The original C89 committee focused on codifying existing practice rather
than inventing new features. Perhaps there weren't enough existing
compilers with min/max implementations.
It is strange enough because all old C compilers had usually min/max
macros. And it is funny that even now users using modern C compilers
define min/max macros yourself. It looks like a gap in the C
standard because it does not offer an alternative.
Some consider macros evil. I'd even consider those very macros evil because
they can break if the 'arguments' contain side effects. At least they
should be called MIN/MAX instead. Further it isn't hard to write similar
code using the ternary operator. Still, I'm aware that many people seem to
like the macro.
I guess there simply wasn't enough consent for their inclusion but strong
voices against.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschaftsfuhrer: Thorsten Focking, Amtsgericht Hamburg HR B62 932
Duane Hebert
2010-02-01 13:30:31 UTC
Permalink
Post by Vladimir Grigoriev
Ulrich, your position has no any reasonable base. In fact it looks the
following way. You say that macros (as it follows including min/max macros)
are evils. O.k., and what to do to find minimal/maximum values? You
answer: "To write these macros yourself!" Is not it very strange, is it? And
what do compiler producers do? They include these macros in their libraries!
Or they advise users to write these macros themselves.:)
It's not exactly just Ulrich's position .
http://www2.research.att.com/~bs/bs_faq2.html
Vladimir Grigoriev
2010-02-01 13:52:52 UTC
Permalink
Duane, I am not arguing that macros are dangerous. I say that the C standard
did not offer an alternative for the macros max/min. And when somebody asks
what to do the same people advise him to write these macros yourself and use
them! At least as far as I know most projects on C includes these macros in
their code. Moreover the Microsoft itself includes these macros!

I can not find a reference to a document where the C Standard committee
discusses this question. Maybe someone knows such a reference?

Vladimir Grigoriev
Post by Duane Hebert
Post by Vladimir Grigoriev
Ulrich, your position has no any reasonable base. In fact it looks the
following way. You say that macros (as it follows including min/max
macros) are evils. O.k., and what to do to find minimal/maximum values?
You answer: "To write these macros yourself!" Is not it very strange, is
it? And what do compiler producers do? They include these macros in their
libraries! Or they advise users to write these macros themselves.:)
It's not exactly just Ulrich's position .
http://www2.research.att.com/~bs/bs_faq2.html
Duane Hebert
2010-02-01 15:12:52 UTC
Permalink
Post by Vladimir Grigoriev
Duane, I am not arguing that macros are dangerous. I say that the C standard
did not offer an alternative for the macros max/min. And when somebody asks
Ok. I was just pointing out that Stroustrup himself has the same opinion.
Post by Vladimir Grigoriev
what to do the same people advise him to write these macros yourself and use
them! At least as far as I know most projects on C includes these macros in
their code. Moreover the Microsoft itself includes these macros!
Yes, MS does use macros for min/max and it's a pain in the *ss for anyone that
wants to use std::min/std::max. They could have at least used the all caps
format.

But MS uses macros everywhere and so you can't create functions like myclass::CopyFile
without getting errors that CopyFilea or CopyFilew is not a member...
Post by Vladimir Grigoriev
I can not find a reference to a document where the C Standard committee
discusses this question. Maybe someone knows such a reference?
Not sure why C standards haven't changed to add these. It's been a while
since I used straight C and then I had a set of macros that I needed.
Bo Persson
2010-02-01 16:49:14 UTC
Permalink
Post by Vladimir Grigoriev
Duane, I am not arguing that macros are dangerous. I say that the C
standard did not offer an alternative for the macros max/min. And
when somebody asks what to do the same people advise him to write
these macros yourself and use them! At least as far as I know most
projects on C includes these macros in their code. Moreover the
Microsoft itself includes these macros!
But they are not part of the compiler or standard library, they are
part of the Windows headers (an application).


Bo Persson
Vladimir Grigoriev
2010-02-01 17:12:00 UTC
Permalink
Yes! And this demonstrates one more what I said already that C-programmers
have to include these macros in their C projects.
So it would be interesting to read C standard Committee rationale relative
to these macros. I can not find it.

Vladimir Grigoriev
Post by Vladimir Grigoriev
Duane, I am not arguing that macros are dangerous. I say that the C
standard did not offer an alternative for the macros max/min. And
when somebody asks what to do the same people advise him to write
these macros yourself and use them! At least as far as I know most
projects on C includes these macros in their code. Moreover the
Microsoft itself includes these macros!
But they are not part of the compiler or standard library, they are part
of the Windows headers (an application).
Bo Persson
Ulrich Eckhardt
2010-02-01 18:08:41 UTC
Permalink
Post by Vladimir Grigoriev
Ulrich, your position has no any reasonable base. In fact it looks the
following way. You say that macros (as it follows including min/max
macros) are evils.
Right. There are various reasons macros are evil, see e.g. the C++ FAQ at
parashift's.
Post by Vladimir Grigoriev
O.k., and what to do to find minimal/maximum values? You answer: "To
write these macros yourself!" Is not it very strange, is it?
Yes, it is strange that I get misquoted. The real quote of what I said
is: "[...]it isn't hard to write similar code using the ternary operator."
That doesn't mean that you should put that code into a macro. Putting it
into a function is one alternative, though lack of overloading makes it
cumbersome in C. The other alternative is obviously to use the ternary
operator directly.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Vladimir Grigoriev
2010-02-01 19:22:34 UTC
Permalink
If you will start to use the conditional operator directly you will finish
with witting the macros!:) You have forgotten that programmers passed
this way already. Otherwise why appeared the macros in C and why producers
of compilers include them till now?

Vladimir Grigoriev
Post by Ulrich Eckhardt
Post by Vladimir Grigoriev
Ulrich, your position has no any reasonable base. In fact it looks the
following way. You say that macros (as it follows including min/max
macros) are evils.
Right. There are various reasons macros are evil, see e.g. the C++ FAQ at
parashift's.
Post by Vladimir Grigoriev
O.k., and what to do to find minimal/maximum values? You answer: "To
write these macros yourself!" Is not it very strange, is it?
Yes, it is strange that I get misquoted. The real quote of what I said
is: "[...]it isn't hard to write similar code using the ternary operator."
That doesn't mean that you should put that code into a macro. Putting it
into a function is one alternative, though lack of overloading makes it
cumbersome in C. The other alternative is obviously to use the ternary
operator directly.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschaftsfuhrer: Thorsten Focking, Amtsgericht Hamburg HR B62 932
Ulrich Eckhardt
2010-02-02 08:00:55 UTC
Permalink
Post by Vladimir Grigoriev
If you will start to use the conditional operator directly you will finish
with witting the macros!:)
No I wont, because I understand the dangers of macros.
Post by Vladimir Grigoriev
You have forgotten that programmers passed this way already.
Otherwise why appeared the macros in C and why producers of
compilers include them till now?
Can you tell me where to find it in the latest GCC release?

[Snipped TOFU]

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Vladimir Grigoriev
2010-02-02 11:39:14 UTC
Permalink
As I guess GCC is a pure compiler. Nevertheless most C programmers which use
it write the macros in their C projects. The macros are selfdocumented as
opposed to the conditional operator.

Vladimir Grigoriev
Post by Ulrich Eckhardt
Post by Vladimir Grigoriev
If you will start to use the conditional operator directly you will finish
with witting the macros!:)
No I wont, because I understand the dangers of macros.
Post by Vladimir Grigoriev
You have forgotten that programmers passed this way already.
Otherwise why appeared the macros in C and why producers of
compilers include them till now?
Can you tell me where to find it in the latest GCC release?
[Snipped TOFU]
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Geschaftsfuhrer: Thorsten Focking, Amtsgericht Hamburg HR B62 932
Ulrich Eckhardt
2010-02-02 12:25:09 UTC
Permalink
Post by Vladimir Grigoriev
As I guess GCC is a pure compiler. Nevertheless most C programmers which
use it write the macros in their C projects.
Yes, sure. And 95% of all statistic claims on the Usenet are made up out of
thin air just to prove a point. /sarcasm

BTW: I specifically asked about GCC because it has a 'typeof' extension,
which allows creating macro-functions (a bastard between those two) that
don't evaluate the macro arguments each and every time they are used. Then,
they behave like C++ template functions rather and letting them keep their
function look (lowercase instead of uppercase for macros) stops being a
danger.

Alas, even though they could provide a safe version of min/max, the GCC
people chose not to.
Post by Vladimir Grigoriev
The macros are selfdocumented
as opposed to the conditional operator.
Nope. They look like functions, which is the first sin and what makes them
definitely non-selfdocumented. Further, which element do they yield when
the two passed arguments are equal? Also, do they return l-values or
r-values?

Snipped further useless amounts of TOFU, were you actually quoting that for
a reason?

Cheers!

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Tim Roberts
2010-02-03 05:39:46 UTC
Permalink
Post by Vladimir Grigoriev
As I guess GCC is a pure compiler.
I don't know what you were actually trying to say, but that sentence as you
wrote it is utter nonsense. Gcc is just a compiler, exactly as "pure" as
Visual C++.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Vladimir Grigoriev
2010-02-03 13:06:55 UTC
Permalink
No, Visual C++ is not just a compiler. It is an integrated system which
includes for example MFC,

Vladimir Grigoriev
Post by Tim Roberts
Post by Vladimir Grigoriev
As I guess GCC is a pure compiler.
I don't know what you were actually trying to say, but that sentence as you
wrote it is utter nonsense. Gcc is just a compiler, exactly as "pure" as
Visual C++.
--
Providenza & Boekelheide, Inc.
Bo Persson
2010-02-03 17:19:27 UTC
Permalink
Post by Vladimir Grigoriev
No, Visual C++ is not just a compiler. It is an integrated system
which includes for example MFC,
Vladimir Grigoriev
That's the Visual Studio, right? If you use the Express edition, you
don't get MFC.

The compiler is a separate program, just like gcc.


Bo Persson
Post by Vladimir Grigoriev
Post by Tim Roberts
Post by Vladimir Grigoriev
As I guess GCC is a pure compiler.
I don't know what you were actually trying to say, but that
sentence as you
wrote it is utter nonsense. Gcc is just a compiler, exactly as
"pure" as Visual C++.
--
Providenza & Boekelheide, Inc.
Continue reading on narkive:
Loading...