Visual C++ 将流读取为双精度时的奇怪行为

use*_*863 9 c++ double runtime stream

我在 Visual Studio 2022(版本 17.7.1)中遇到一个奇怪的问题。在下面的程序中:

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    istringstream istr("1e-07 3");
    double xx = 0;
    int yy = 0;
    istr >> xx >> yy;
    cout << "xx = " << xx << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序将根据运行时库和平台工具集进行不同的打印:

Toolset v143, Multi-threaded Debug or Multi-threaded: 1e-06
Toolset V143, Multi-threaded Debug DLL or Multi-threaded DLL: 1e-07
Toolset v142, Multi-threaded Debug or Multi-threaded: 1e-07
Toolset V142, Multi-threaded Debug DLL or Multi-threaded DLL: 1e-07
Run Code Online (Sandbox Code Playgroud)

有人可以证实这一点吗?如果这是编译器中的一个实际错误,我会感到惊讶。

谢谢。

Ala*_*les 1

_Parse_fp_with_locale中的函数xlocnum正在返回"010e-7",而使用调试 DLL 运行时它返回正确的值"01e-7"。有人已经提交了修复程序https://github.com/microsoft/STL/pull/3982。现在,您可以通过不在指数上添加前导零来解决该问题。

如果您真的很勇敢,那么注释掉第 1018 行的以下块xlocnum似乎可以解决问题:

if (_Seendigit) {
    *_Ptr++ = '0'; // put one back
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio 2022 17.9 将包含此修复