std::from_chars 应该处理大写的十六进制指数吗?

Is std::from_chars supposed to handle uppercase hexadecimal exponents?

升级到 Ubuntu 22.04 (amd64) 后,我注意到以下代码开始给出结果 1.4375 而不是预期值 1472:

#include <charconv>
#include <iostream>
#include <string_view>

int main()
{
    std::string_view src{"1.7P10"};
    double value;
    auto result = std::from_chars(src.data(), src.data() + src.size(), value, std::chars_format::hex);
    std::cout << value << '\n';
}

如果我将源代码中的 P 字符更改为小写 p,我会得到预期的结果。大写和小写均适用于 std::strtod.

std::from_chars 是否应该因大写指数字符而失败,或者这是 g++/libstdc++ 中的错误?

用于编译的命令行:

g++ -std=c++17 test.cpp

(优化级别似乎没有效果)

g++ --version 的输出:

g++ (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Is std::from_chars supposed to fail with uppercase exponent characters, or is this a bug in g++/libstdc++?

这是libstdc++的bug,已提交105441

来自[charconv.from.chars],强调我的

from_chars_result from_chars(const char* first, const char* last, double& value,
                             chars_format fmt = chars_format::general);
  • Preconditions: fmt has the value of one of the enumerators of chars_­format.
  • Effects: The pattern is the expected form of the subject sequence in the "C" locale, as described for strtod, except that
    • the sign '+' may only appear in the exponent part;
    • if fmt has chars_­format​::​scientific set but not chars_­format​::​fixed, the otherwise optional exponent part shall appear;
    • if fmt has chars_­format​::​fixed set but not chars_­format​::​scientific, the optional exponent part shall not appear; and
    • if fmt is chars_­format​::​hex, the prefix "0x" or "0X" is assumed.

所以在你的例子中,"1.7P10" 应该是一个有效的模式,from_chars 的结果应该等同于 strtod("0x1.7P10").