csv-parser在C++17下from_chars函数问题
前言:
哎!c++碎片化,真是麻烦!
我win下vs2017的C++17没问题.
linux下 g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0有问题.即使c++17
这就牵扯到各家编译时实现的不同.
解决办法:
参考我的
https://github.com/vincentlaucsb/csv-parser/issues/257#issuecomment-2642703294
C++17的std::from_chars最初只支持整数类型,比如int、long等,而对浮点数类型的支持是在C++20中添加的。所以可能在C++17中,确实没有针对浮点数的from_chars函数,导致编译器找不到对应的重载版本,从而报错。
用这个函数代替原先的get_max函数
long double get_max(std::string file, std::string column, bool use_std = false);
long double get_max(std::string file, std::string column, bool use_std) {
using namespace csv;
long double max = -std::numeric_limits<long double>::infinity();
CSVReader reader(file);
for (auto& row : reader) {
auto field = row[column];
long double out = 0;
if (use_std) {
auto _field = field.get<std::string_view>();
const char* data = _field.data();
char* end;
errno = 0; // 重置错误标志
out = std::strtold(data, &end);
// 检查转换是否成功
if (data == end) {
// 没有数字被转换,处理错误
std::cerr << "转换失败: '" << _field << "'" << std::endl;
continue; // 跳过当前行
} else if (errno == ERANGE) {
// 处理溢出情况
if (out == HUGE_VALL) {
out = std::numeric_limits<long double>::infinity();
} else if (out == -HUGE_VALL) {
out = -std::numeric_limits<long double>::infinity();
}
}
} else {
out = field.get<long double>();
}
if (out > max) {
max = out;
}
}
return max;
}