简体   繁体   中英

min and max in legacy C++

I am trying to compile some older C++ code (probably from around 2001-2002) on a Debian GNU/Linux stable system. When compiling, I get the error:

In file included from /usr/include/c++/4.7/vector:66:0,
                 from ../FooMath/FooBar.h:23,
                 from FooBar.cpp:2:
/usr/include/c++/4.7/bits/stl_bvector.h: In member function ‘std::vector<bool, _Alloc>::size_type std::vector<bool, _Alloc>::max_size() const’:
/usr/include/c++/4.7/bits/stl_bvector.h:685:2: error: ‘max’ is not a member of ‘__gnu_cxx::__numeric_traits<long int>’

Hunting down the problem, I came across this piece of code in one of our own header files, which I assume is related to this problem:

#if defined(IRIX) | linux
signed  max(signed a, signed b);
long    max(long a, long b);
double  max(double a, double b);
float   max(float a, float b);
signed  min(signed a, signed b);
long    min(long a, long b);
double  min(double a, double b);
#define  __min min
#define  __max max
float   min(float a, float b);
#endif

and at lots of other places, I see calls to a __max() function with two arguments.

My educated guess is that I can replace all these calls to __max() with calls to std::max() and that I should include the <algorithm> header.

My two questions are:

  1. Is my educated guess correct?
  2. I assume the extra declarations and defines of min and max are some historical leftover. Does anyone know the history of it? Why was this code ever needed?

At the line 685 in bits/stl_bvector.h you have :

    __gnu_cxx::__numeric_traits<difference_type>::__max

Your ugly macro is replacing __max by max , hence the error.

You can effectively replace all your __max in your code by std::max , but just to be sure not to dismantle everything, you could first rename __max by __my__max , just to be sure (in your macro too). That way you will keep using the self-defined min/max functions.


Another solution would be to undef your macro before including stl headers and reenabling it after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM