Post by Peter C. ChapinPost by Evgeny KotsubaC 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. ChapinHandling 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. ChapinPost by Evgeny KotsubaMax/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 KotsubaMoreover, 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