Visual C++ 2017可正常编译调用自定义log的非歧义标识符技术问询
整理后的可编译代码示例
这段带BOM的UTF-8编码代码能在Visual C++ 2017中无警告编译,且自定义log函数调用完全没有标识符歧义问题,完整代码如下:
// Source encoding: UTF-8 with BOM ∩ #include <algorithm> // std::for_each #include <iostream> #include <math.h> // ::(sin, cos, atan,..., log) #include <string> // std::string void log( std::string const& message ) { std::clog << "-- log entry: " << message << std::endl; } auto main() -> int { auto const messages = { "Blah blah...", "Duh!", "Oki doki" }; std::for_each( messages.begin(), messages.end(), log ); return 0; }
关键细节说明
- 选用
<math.h>而非<cmath>:数学库中的log函数会被放在全局命名空间中,而自定义的log是接受std::string参数的全局函数,参数类型差异彻底避免了标识符歧义。 - Visual C++ 2017对带BOM的UTF-8编码支持完善,不会出现编码解析错误。
- 代码采用C++11及以上的语法特性(如
auto返回值、初始化列表),编译器完全兼容。
内容的提问来源于stack exchange,提问作者Cheers and hth. - Alf
相关产品推荐
相关产品推荐

