2

I noticed inconsistent behavior when converting a malformed string to a double value. Consider the following code:

#include <iostream>
#include <sstream>

int main (void)
{
    std::stringstream ss("10.2345asdf");
    double ret;
    ss >> ret;

    // Outputs 10.2345 on Linux, 0 on MacOS
    std::cout << ret << std::endl;  

    return 0;
}

On Linux, the program outputs 10.2345, which is consistent with conversions of malformed strings to int, e.g., 10asdf will be converted to 10. On MacOS, however, the program outputs 0, which is inconsistent with Linux and inconsistent with how ints are converted using this technique.

It is interesting to see std::stof and std::stod being integrated into the C++11 standard. Are these new commands related to this issue?

On Linux, I tried both g++ and clang++. On Apple I used clang with LLVM version 6.1.0.

crazypeter
  • 1,279
  • 1
  • 10
  • 14

0 Answers0