为什么<T>编译器将 numeric_limits::min 解释为函数指针?

why is numeric_limits<T>::min being interpreted as a function pointer by compiler?

本文关键字:解释 min 指针 limits 函数 numeric lt gt 编译器 为什么      更新时间:2023-10-16

我正在使用模板编写一个浮点比较函数:

template<class T>
    static bool AlmostEqual(T f1, T f2) 

我有这样一个语句:

T min_value = std::numeric_limits<T>::min();

由于某种原因,编译器将上面的最小值视为函数指针并发出错误:

   ../include/CommonFunctions.h: In static member function ‘static bool CommonFunctions::AlmostEqual(T, T) [with T = double]’:
file.cpp:2200:   instantiated from here
../include/CommonFunctions.h:22: error: cannot convert ‘double (*)()throw ()’ to ‘double’ in initialization

静态函数被定义为大程序的一部分。但是,当我将相同的函数放在一个独立的文件中并编译和使用它时,我没有看到任何错误。我读了其他人的帖子,试着这样做:

T min_value(std::numeric_limits<T>::min());

我仍然得到相同的错误,而且还有一个:

../include/CommonFunctions.h:22:53: error: macro "min" requires 2 arguments, but only 1 given

在一个错误中,它能够正确地将min解析为函数,而在另一个错误中,它被视为宏。我不确定如何解决这个问题。有什么建议吗?

对于包括windows.h在内的人,请在有效的头文件中放入以下内容:

#include windows headers ...
pragma push_macro("min")
pragma push_macro("max")
#undef min
#undef max
#include headers expecting std::min/std::max ...
...
pragma pop_macro("min")
pragma pop_macro("max")

在源文件中只需要#undef和max.

#include windows headers ...
#undef min
#undef max
#include headers expecting std::min/std::max ...