簡體   English   中英

遺留C ++中的最小和最大

[英]min and max in legacy C++

我正在嘗試在Debian GNU / Linux穩定系統上編譯一些舊的C ++代碼(可能在2001-2002年左右)。 編譯時出現錯誤:

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>’

為了解決這個問題,我在自己的一個頭文件中遇到了這段代碼,我認為與該問題有關:

#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

在其他很多地方,我看到了帶有兩個參數的__max()函數的調用。

我的有根據的猜測是,我可以將所有對__max()的調用替換為對std::max()調用,並且應該包含<algorithm>標頭。

我的兩個問題是:

  1. 我的猜測正確嗎?
  2. 我假設多余的聲明和min和max的定義是一些歷史遺留的。 有人知道它的歷史嗎? 為什么需要此代碼?

bits/stl_bvector.h的第685行,您具有:

    __gnu_cxx::__numeric_traits<difference_type>::__max

您的丑陋宏將__max替換為max ,因此是錯誤。

您可以用std::max有效地替換代碼中的所有__max ,但是為了確保不__max所有內容,可以先通過__my__max重命名__my__max ,以確保(也在宏中)。 這樣,您將繼續使用自定義的最小/最大函數。


另一種解決方案是undef ,包括STL頭和后重新啟用它之前您的宏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM