Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringstream not working with doubles when _GLIBCXX_DEBUG enabled

Tags:

c++

xcode

macos

stl

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.

like image 283
John Stephen Avatar asked Oct 01 '11 21:10

John Stephen


1 Answers

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.

like image 104
Marshall Clow Avatar answered Oct 17 '22 14:10

Marshall Clow