启用 _GLIBCXX_DEBUG 时,Stringstream 无法使用双精度数

发布于 2024-12-07 14:02:59 字数 825 浏览 0 评论 0原文

我正在使用 _GLIBCXX_DEBUG 模式来帮助查找代码中的错误,但我遇到了一个问题,我认为这是库中的错误,但希望有人可以告诉我我只是做错了什么。这是一个重现问题的简短示例:

#define _GLIBCXX_DEBUG
#include <iostream>
#include <sstream>

int main (int argc, const char * argv[]) {
    std::ostringstream ostr;
    ostr << 1.2;
    std::cout << "Result: " << ostr.str() << std::endl;
    return 0;
}

如果我注释掉 #define,则输出为(如预期):

Result: 1.2

随着 _GLIBCXX_DEBUG 定义到位,但输出很简单:

Result:

我已将其跟踪到 _M_num_put 字段流保留为 NULL,这会导致在流中抛出(并捕获)异常,并导致没有数字输出。 _M_num_put 应该是来自语言环境的 std::num_put (我并不声称理解它应该如何工作,这只是我迄今为止在搜索中学到的东西)。

我在带有 XCode 的 Mac 上运行它,并尝试使用“LLVM GCC 4.2”和“Apple LLVM Compiler 3.0”作为编译器,得到相同的结果。

我将不胜感激任何帮助解决这个问题的帮助。我想继续在我的代码上以 _GLIBCXX_DEBUG 模式运行,但这会干扰这一点。

I'm using _GLIBCXX_DEBUG mode to help find errors in my code but I'm having a problem which I think is an error in the library, but hopefully someone can tell me I'm just doing something wrong. Here is a short example which repro's the problem:

#define _GLIBCXX_DEBUG
#include <iostream>
#include <sstream>

int main (int argc, const char * argv[]) {
    std::ostringstream ostr;
    ostr << 1.2;
    std::cout << "Result: " << ostr.str() << std::endl;
    return 0;
}

If I comment out the #define then the output is (as expected):

Result: 1.2

With the _GLIBCXX_DEBUG define in place however the output is simply:

Result:

I've tracked this down to the _M_num_put field of the stream being left as NULL, which causes an exception to be thrown (and caught) in the stream and results in no output for the number. _M_num_put is supposed to be a std::num_put from the locale (I don't claim to understand how that's supposed to work, it's just what I've learned in my searching so far).

I'm running this on a Mac with XCode and have tried it with both "LLVM GCC 4.2" and "Apple LLVM Compiler 3.0" as the compiler with the same results.

I'd appreciate any help in solving this. I want to continue to run with _GLIBCXX_DEBUG mode on my code but this is interfering with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丘比特射中我 2024-12-14 14:03:00

其他人在 cplusplus.com 看过此内容
以及这里也是 stackoverflow

共识是,这是 Mac OS 的 gcc 4.2 中的一个已知错误,并且由于该编译器不再更新,因此不太可能被修复。

在我看来,您可以(1)使用 LLVM,或者(2)构建您自己的 GCC 并使用它。

Someone else has seen this over at cplusplus.com
and here at stackoverflow, too.

Consensus is that it is a known bug in gcc 4.2 for Mac OS, and since that compiler is no longer being updated, it is unlikely to ever be fixed.

Seems to me that you can either (1) use LLVM, or (2) build your own GCC and use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文