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函数

cpp 复制代码
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;
}
相关推荐
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛3 天前
delete又未完全delete
c++
端平入洛4 天前
auto有时不auto
c++
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端
norlan_jame5 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20215 天前
信号量和信号
linux·c++
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc