問題:使用std::numeric_limits<int>::max方法時(shí),編譯器報(bào)warning C4003: “max”宏的實(shí)參不足。使用std::max、std::min和定義名為max、min方法時(shí)也報(bào)錯(cuò)。
原因:由于頭文件Windows.h中的定義了宏max和min造成的。在Windows.h頭文件中定義了宏max和min,Preprocessor就認(rèn)為我們使用的是宏max或min,而再調(diào)用時(shí)調(diào)用方法和參數(shù)與定義的宏不一致,所以報(bào)錯(cuò)。
解決方案:用括號(hào)來改變Preprocessor對方法名的理解。
例:
// 使用numeric_limits中的max方法
(std::numeric_limits<Byte>::max)()
// 使用stl中的max方法
(std::max)( 1, 2 );
// 自定義名為max的方法
struct Number
{
int (max)();
};
參見:http://www.jeffhung.net/blog/articles/jeffhung/626/