Visual Studio 的最小和最大函数

Min and max functions with Visual Studio

我的项目的一个头文件(lib.hpp 文件)中有以下代码:

#ifndef SLIM_MATHS_LIB_HPP_
# define SLIM_MATHS_LIB_HPP_

namespace slim
{
namespace maths
{

namespace lib
{
template <typename T>
inline T        min(T a, T b); // Many errors on this line (see below)
// Other functions
}
}
}

# include "lib.ipp" // Functions definitions are inside

#endif // !SLIM_MATHS_LIB_HPP_

它在 GNU/Linux 系统上编译并与 GCC 一起工作得很好。

现在我试图在 Windows 10 上用 Visual Studio 14.0 编译它,但我在 min 函数定义行上遇到了很多错误,如下所示:

Error C2146 syntax error: missing ')' before identifier 'a' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2433 'T': 'inline' not permitted on data declarations slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2365 'T': redefinition; previous definition was 'template parameter' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2061 syntax error: identifier 'a' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2059 syntax error: ')' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2146 syntax error: missing ')' before identifier 'b' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23
Error C2146 syntax error: missing ';' before identifier 'b' slim3d-core C:\Users\msi\Desktop\SLIM3D\inc\slim\maths\lib.hpp 23

我已经在 Windows 7 系统上用 Visual Studio 14.0 成功编译了它,但是 lib.hpplib.ipp 分别命名为 lib.hhlib.hpp,所以我认为这是 Windows 10 的系统问题或扩展问题。
也许 Visual Studio 拒绝将 .ipp 文件的内容视为 C++ 代码,因为当我打开它时它不会将其着色为代码。但是,由于它包含在 .hpp 文件中,而不是直接添加到解决方案中,因此应该没有区别。

min 定义为 windows.h 中的宏。在声明函数之前向 header 添加 #undef min 行。