Discussion:
max/min macros are not been declared in stdlib.h for C++
(too old to reply)
Evgeny Kotsuba
2006-01-28 05:01:46 UTC
Permalink
#include <stdlib.h>
int a=5, b=7;
int main(void)
{ int c,d;
c = max(a,b);
d = min(a,b);
}
/////////
wpp386 test.cpp
test.cpp(6): Error! E029: col(8) symbol 'max' has not been declared
test.cpp(7): Error! E029: col(8) symbol 'min' has not been declared
This is intentional. The macro max is not standard and it causes
problems in C++ because of it's poor interactions with std::max and the
max members of std::numeric_limits. (This is under the general heading
of "macros are bad") If you want to use max in a C++ program use
std::max from <algorithm>.
Does this seem like a reasonable resolution?
No.
C and C++ should be compatible,period.
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine. What are then with more complicated
things ?!!!
Moreover, Max/min macros are placed inside WATCOM\h\cstdlib that is
included from stdlib.h....

SY,
EK
Peter C. Chapin
2006-01-28 05:01:45 UTC
Permalink
#include <stdlib.h>
int a=5, b=7;
int main(void)
{ int c,d;
c = max(a,b);
d = min(a,b);
}
/////////
wpp386 test.cpp
test.cpp(6): Error! E029: col(8) symbol 'max' has not been declared
test.cpp(7): Error! E029: col(8) symbol 'min' has not been declared
This is intentional. The macro max is not standard and it causes
problems in C++ because of it's poor interactions with std::max and the
max members of std::numeric_limits. (This is under the general heading
of "macros are bad") If you want to use max in a C++ program use
std::max from <algorithm>.

Does this seem like a reasonable resolution?

Peter
Evgeny Kotsuba
2006-01-28 05:01:46 UTC
Permalink
Post by Evgeny Kotsuba
#include <stdlib.h>
int a=5, b=7;
int main(void)
{ int c,d;
c = max(a,b);
d = min(a,b);
}
/////////
wpp386 test.cpp
test.cpp(6): Error! E029: col(8) symbol 'max' has not been declared
test.cpp(7): Error! E029: col(8) symbol 'min' has not been declared
This is intentional. The macro max is not standard and it causes
problems in C++ because of it's poor interactions with std::max and
the max members of std::numeric_limits. (This is under the general
heading of "macros are bad") If you want to use max in a C++ program
use std::max from <algorithm>.
Does this seem like a reasonable resolution?
No.
C and C++ should be compatible,period.
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine. What are then with more complicated
things ?!!!
Moreover, Max/min macros are placed inside WATCOM\h\cstdlib that is
included from stdlib.h....
/* FILE: stdlib.h/cstdlib (Standard Library functions) */
...
#ifndef __cplusplus
#error The header cstdlib requires C++
#endif
.....
/* min and max macros */
#if !defined(max) && !defined(__cplusplus)
^^^^^^^^^^^^^^^^^^^^^^^^^^
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
---------------8<---------------------------
* OWTroll is falling from the oak


On the othe hand, looking at watcomXXX directories I see that this
monster constuction was introduced ad Watcom 11...

SY,
EK
Peter C. Chapin
2006-01-28 05:01:47 UTC
Permalink
Post by Evgeny Kotsuba
C and C++ should be compatible,period.
C and C++ aren't 100% compatible. For example C programs that do implict
conversions of void* to other pointer types won't compile as C++.
Similarly C programs that have global const variables and that then
assume those variables have external linkage won't work as C++ either.
There are other incompatibilities pertaining to the way functions are
declared. As a result it is normal for a move from C to C++ to require a
few tweaks in the code. Handling of max could be regarded as just
another one of those tweaks.
Post by Evgeny Kotsuba
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine.
Min and max macros are not part of the C standard (that I could see...
did I miss it?). Thus a portable C program should provide its own
version of those macros. Their definition in stdlib.h is an extension.

I can see the value in C++ supporting the extensions available in the C
library. Such support could be arranged in this case by introducing a
global function max() that is only visible when stdlib.h is compiled by
the C++ compiler. However that would conflict with similar definitions
in third party headers (for example, older C++ code) and the

#if !defined(max)
#endif

trick wouldn't work for a function (introducing max as a macro causes
even bigger problems). So there isn't really a great resolution.
Considering that Standard C++ *does* provide a max function in namespace
std, I'm inclined to say that people should just use that.
Post by Evgeny Kotsuba
Moreover, Max/min macros are placed inside WATCOM\h\cstdlib that is
included from stdlib.h....
Both cstdlib and stdlib.h are generated from a common source file. This
causes some odd redundancies but they are not worth the trouble to
remove.

Peter
Evgeny Kotsuba
2006-01-28 05:01:48 UTC
Permalink
Post by Peter C. Chapin
Post by Evgeny Kotsuba
C and C++ should be compatible,period.
C and C++ aren't 100% compatible. For example C programs that do implict
conversions of void* to other pointer types won't compile as C++.
Similarly C programs that have global const variables and that then
assume those variables have external linkage won't work as C++ either.
There are other incompatibilities pertaining to the way functions are
declared. As a result it is normal for a move from C to C++ to require a
few tweaks in the code.
But after those work the same code compiled by C as well by C++ compiler
Post by Peter C. Chapin
Handling of max could be regarded as just
another one of those tweaks.
Well, hands up. I look in two big open source projects and both have
self-declarated max()/min() macros...
Post by Peter C. Chapin
Post by Evgeny Kotsuba
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine.
Min and max macros are not part of the C standard (that I could see...
did I miss it?). Thus a portable C program should provide its own
version of those macros. Their definition in stdlib.h is an extension.
I can see the value in C++ supporting the extensions available in the C
library. Such support could be arranged in this case by introducing a
global function max() that is only visible when stdlib.h is compiled by
the C++ compiler. However that would conflict with similar definitions
in third party headers (for example, older C++ code) and the
#if !defined(max)
#endif
trick wouldn't work for a function (introducing max as a macro causes
even bigger problems). So there isn't really a great resolution.
Considering that Standard C++ *does* provide a max function in namespace
std, I'm inclined to say that people should just use that.
Post by Evgeny Kotsuba
Moreover, Max/min macros are placed inside WATCOM\h\cstdlib that is
included from stdlib.h....
Both cstdlib and stdlib.h are generated from a common source file. This
causes some odd redundancies but they are not worth the trouble to
remove.
Look at IBMCPP40\include\stdlib.h
---------------8<-----------------------------
#if __IBMCPP__<400 || __COMPAT_MAXMIN__
#ifndef max
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#else
#if !defined __templatemaxmin
#define __templatemaxmin
extern "C++" {
namespace std {
template<class _Ty> inline
const _Ty& max(const _Ty& _X, const _Ty& _Y)
{ return (_X < _Y ? _Y : _X); }

template<class _Ty> inline
const _Ty& min(const _Ty& _X, const _Ty& _Y)
{ return (_Y < _X ? _Y : _X); }
}
}
#endif
using std::max;
using std::min;
#endif
---------------8<-----------------------------

This has both compatible and ++ variants.


SY,
EK
Peter C. Chapin
2006-01-28 05:01:55 UTC
Permalink
Post by Evgeny Kotsuba
But after those work the same code compiled by C as well by C++ compiler
I'm not sure I understand your objection to including <algorithm> and
using std::min and std::max in your C++ program. It's standard and it's
portable.
Post by Evgeny Kotsuba
Well, hands up. I look in two big open source projects and both have
self-declarated max()/min() macros...
Are you saying that these projects provided their own min and max? If
so, doesn't that support the idea that the application should provide
these macros and not the library headers? I must be misundertanding your
point.
Post by Evgeny Kotsuba
Look at IBMCPP40\include\stdlib.h
---------------8<-----------------------------
#if __IBMCPP__<400 || __COMPAT_MAXMIN__
#ifndef max
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
#else
#if !defined __templatemaxmin
#define __templatemaxmin
extern "C++" {
namespace std {
template<class _Ty> inline
const _Ty& max(const _Ty& _X, const _Ty& _Y)
{ return (_X < _Y ? _Y : _X); }
template<class _Ty> inline
const _Ty& min(const _Ty& _X, const _Ty& _Y)
{ return (_Y < _X ? _Y : _X); }
}
}
#endif
using std::max;
using std::min;
#endif
---------------8<-----------------------------
Something like this could certainly be done. I'm not sure if it's worth
it or not (maybe so). In any case the lack of min/max in stdlib.h is
definitely not a "bug" since no formal definition of C++ (or C for that
matter) requires it. Perhaps you are asking that this extension in the C
library be added to the C++ library as well. However, this gets back to
the question: why is it necessary? It's not like there is no library
min/max at all in C++.

Peter
Michal Necasek
2006-01-28 05:01:48 UTC
Permalink
Post by Evgeny Kotsuba
C and C++ should be compatible,period.
That is your opinion. It is notably *not* the view of the C++
standards committee. I strongly suggest that if you don't like the way
the C++ standard is defined, you complain to the C++ standards
committee, not to us. We're only implementors, we do not decide what is
or isn't part of the C++ language.

The fact is that C and C++ aren't compatible. You'll have to live with it.
Post by Evgeny Kotsuba
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine.
Maybe you only can't imagine the gotchas because you don't know C++
that well? To loosely paraphrase P.J. Plauger, an expert is someone who
can think of all the things that can go wrong...

FYI, the 'min' and 'max' macros aren't part of the C standard either
and are only supplied in stdlib.h as a concession to existing practice.


Michal
Evgeny Kotsuba
2006-01-28 05:01:50 UTC
Permalink
Post by Evgeny Kotsuba
C and C++ should be compatible,period.
That is your opinion. It is notably *not* the view of the C++ standards
committee. I strongly suggest that if you don't like the way the C++
standard is defined, you complain to the C++ standards committee, not to
us. We're only implementors, we do not decide what is or isn't part of
the C++ language.
The fact is that C and C++ aren't compatible. You'll have to live with it.
The C and C++ are not mutial compatible. But
C Compatibility
C++ is backwards compatible with the C language. Any code written
in C can easily be included in a C++ program without hardly making any
change.
Post by Evgeny Kotsuba
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine.
Maybe you only can't imagine the gotchas because you don't know C++
that well? To loosely paraphrase P.J. Plauger, an expert is someone who
can think of all the things that can go wrong...
Well, you think you are expert - then there are things that go wrong
with OW and don't go wrong with VAC and other compilers.

I include <sdtlib.h> and it should have max/min macros. Use any
reference to C/C++ and sdtlib.h, use google and look at first thing what
it find: C++ reference: cstdlib library (stdlib.h) at
http://www.cplusplus.com/ref/cstdlib/
FYI, the 'min' and 'max' macros aren't part of the C standard either
and are only supplied in stdlib.h as a concession to existing practice.
for about 20 years as minimum

SY,
EK
Roald Ribe
2006-01-28 05:01:51 UTC
Permalink
Post by Evgeny Kotsuba
Post by Michal Necasek
FYI, the 'min' and 'max' macros aren't part of the C standard either
and are only supplied in stdlib.h as a concession to existing practice.
for about 20 years as minimum
For your information, the page you refer to has a '*' next to min and max.
At the bottom of the page the text:
* = not ANSI-C, but supported by most compilers.

Which pretty much supports what has been said in this thread.

Roald
Evgeny Kotsuba
2006-01-28 05:01:53 UTC
Permalink
Post by Roald Ribe
Post by Evgeny Kotsuba
Post by Michal Necasek
FYI, the 'min' and 'max' macros aren't part of the C standard either
and are only supplied in stdlib.h as a concession to existing practice.
for about 20 years as minimum
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Post by Roald Ribe
For your information, the page you refer to has a '*' next to min and max.
* = not ANSI-C, but supported by most compilers.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Post by Roald Ribe
Which pretty much supports what has been said in this thread.
SY,
EK
JimS
2006-01-28 05:01:52 UTC
Permalink
Post by Evgeny Kotsuba
Post by Evgeny Kotsuba
C and C++ should be compatible,period.
That is your opinion. It is notably *not* the view of the C++ standards
committee. I strongly suggest that if you don't like the way the C++
standard is defined, you complain to the C++ standards committee, not to
us. We're only implementors, we do not decide what is or isn't part of
the C++ language.
The fact is that C and C++ aren't compatible. You'll have to live with it.
The C and C++ are not mutial compatible. But
C Compatibility
C++ is backwards compatible with the C language. Any code written
in C can easily be included in a C++ program without hardly making any
change.
That's just plain wrong. C is not a subset of C++.
Post by Evgeny Kotsuba
Post by Evgeny Kotsuba
Max/min macros are used for years....And is very simple, how it can
causes any problem I can't imagine.
Maybe you only can't imagine the gotchas because you don't know C++
that well? To loosely paraphrase P.J. Plauger, an expert is someone who
can think of all the things that can go wrong...
Well, you think you are expert - then there are things that go wrong
with OW and don't go wrong with VAC and other compilers.
Imagine I had implemented max and min as functions in my own code.
That would cease to compile because of the macros in stdlib.h.
Imagine then, I removed my functions and used the macros in stdlib.h
instead. Then I might have done something like

int a=5,b=6;
c = max(a++,b++);

What happens then if the macro is

#define max(x,y) ((x)>(y)?(x):(y))

What are the values of a and b and c?
Why is that OW specific?

#include <stdio.h>

#define max(x,y) ((x)>(y)?(x):(y))
//int max(int x, int y) { return x>y?x:y; }

int main(void)
{
int a=5,b=6,c;
c = max(a++,b++);
printf("%d %d %d\n", a,b,c);
return 0;
}
Post by Evgeny Kotsuba
I include <sdtlib.h> and it should have max/min macros.
No, it shouldn't. It's not a bug. If you want min and max then put
them in your own code.
Post by Evgeny Kotsuba
Use any
reference to C/C++ and sdtlib.h, use google and look at first thing what
it find: C++ reference: cstdlib library (stdlib.h) at
http://www.cplusplus.com/ref/cstdlib/
<quote>
* max Return the greater of two parameters
* min Return the smaller of two parameters

* = not ANSI-C, but supported by most compilers.

JimS
Michal Necasek
2006-01-28 05:01:52 UTC
Permalink
Post by Evgeny Kotsuba
I include <sdtlib.h> and it should have max/min macros.
Says who? Can you please provide some international standard,
preferably an ISO one, that says so? There's absolutely no mention of
min and max in ISO C99 that I can find - I'm sure you'll be happy to
correct me if I'm wrong. ISO C++ does mention min and max templates -
but guess what, they're in <algorith>, not in <cstdlib>.


Michal
Michal Necasek
2006-01-28 05:01:53 UTC
Permalink
I am not fun of reading ISO standards,
That is your problem. They are very useful, because they tell you what
a standards-compliant compiler is (and isn't) required to support.
All C compilers have its own extensions and all have min/max
What can I say - you're wrong. For instance the EMX GCC OS/2 port does
not define min and max macros for C, and I can't find them in stdlib.h
for InnoTek GCC 3.2.2 either. You should not make sweeping statements
("all C compilers have min/max") unless you're able to back them up with
facts.
but then there is some problem with C and sdtlib.h compatibility - those
templates should be inline when I use #include <stdlib.h> and there
should not be dead code for max/min in <cstdlib> as well
True, cstdlib should not have the min/max macros. Maybe the OW C++
maintainers will fix that.


Michal
Evgeny Kotsuba
2006-01-28 05:01:54 UTC
Permalink
Post by Michal Necasek
I am not fun of reading ISO standards,
That is your problem. They are very useful, because they tell you what
a standards-compliant compiler is (and isn't) required to support.
All C compilers have its own extensions and all have min/max
Ok, "All C compilers I _dealt_ with have its blablabla...."
Post by Michal Necasek
What can I say - you're wrong. For instance the EMX GCC OS/2 port does
not define min and max macros for C, and I can't find them in stdlib.h
for InnoTek GCC 3.2.2 either. You should not make sweeping statements
("all C compilers have min/max") unless you're able to back them up with
facts.
MS QC v1,v2 MSC 4,5,6, MS VC 1, 1.5 etc
TurboC & Borland C (?), Watcom
hmm, some compilers for DSP chips have no min/max
Post by Michal Necasek
but then there is some problem with C and sdtlib.h compatibility -
those templates should be inline when I use #include <stdlib.h> and
there should not be dead code for max/min in <cstdlib> as well
True, cstdlib should not have the min/max macros. Maybe the OW C++
maintainers will fix that.
Ups...
You are not OW C++ maintainer ?


SY,
EK
JimS
2006-01-28 05:01:54 UTC
Permalink
Post by Evgeny Kotsuba
Post by Michal Necasek
I am not fun of reading ISO standards,
That is your problem. They are very useful, because they tell you what
a standards-compliant compiler is (and isn't) required to support.
All C compilers have its own extensions and all have min/max
Ok, "All C compilers I _dealt_ with have its blablabla...."
Post by Michal Necasek
What can I say - you're wrong. For instance the EMX GCC OS/2 port does
not define min and max macros for C, and I can't find them in stdlib.h
for InnoTek GCC 3.2.2 either. You should not make sweeping statements
("all C compilers have min/max") unless you're able to back them up with
facts.
MS QC v1,v2 MSC 4,5,6, MS VC 1, 1.5 etc
TurboC & Borland C (?), Watcom
But not, say, a modern compiler like VC6 or VC.NET2003?
Why don't you use __max and __min? They're supported on OW. And on
VC6 and VC.NET.

I don't see max() and min() in the manual for glibc.
http://www.gnu.org/software/libc/manual/html_node/Function-Index.html

JimS
Wilton Helm
2006-01-31 05:04:42 UTC
Permalink
There are quite a number of "maintainers", all volunteers, all doing what
they have time for, and what they are interested in, or need for themselves.
Allegations or demands are a pretty good way to get ignored. This project
lives (and thrives as a valuable set of tools) because of people who see
problems, figure out how to solve them and test and submit the solution.
Some have been quite good at resolving real problems pointed out by others,
which has merit since each has a special part they are more familiar with,
but lengthy exchanges about trivia only get in the way of real work.

Wilton

Evgeny Kotsuba
2006-01-28 05:01:52 UTC
Permalink
Post by Michal Necasek
Post by Evgeny Kotsuba
I include <sdtlib.h> and it should have max/min macros.
Says who? Can you please provide some international standard,
preferably an ISO one, that says so? There's absolutely no mention of
min and max in ISO C99 that I can find - I'm sure you'll be happy to
correct me if I'm wrong.
I am not fun of reading ISO standards, I have enough other problems
say, with ACPI standard and real practics. I have read a number of tech
books, references etc, deal with a great number of C/C++ source and
reasonal number of C compilers and a bit smaller - C++.
All C compilers have its own extensions and all have min/max
Post by Michal Necasek
ISO C++ does mention min and max templates -
but guess what, they're in <algorith>, not in <cstdlib>.
but then there is some problem with C and sdtlib.h compatibility - those
templates should be inline when I use #include <stdlib.h> and there
should not be dead code for max/min in <cstdlib> as well

SY,
EK
Loading...