Problem with std::ostringstream

Posted by Dony on Stack Overflow See other posts from Stack Overflow or by Dony
Published on 2010-03-29T16:00:24Z Indexed on 2010/03/29 18:43 UTC
Read the original article Hit count: 341

Filed under:

Hi, I tried to make the code #1 more terse by changing it to code #2 but it doesn't work as expected. Can anyone please tell me why it doesn't work? Thanks.

Code #1

double x = 8.9, y = 3.4, z = -4.5;
std::ostringstream q0;
q0 << "(" << x << "," << y << "," << z << ")";
std::string s = q0.str();


Code #2

double x = 8.9, y = 3.4, z = -4.5;
std::string s = static_cast<std::ostringstream &>(
  std::ostringstream() << "(" << x << "," << y << "," << z << ")").str();


EDIT :

Even code #3 works. Then why does code #2 not?

Code #3

double x = 8.9, y = 3.4, z = -4.5;
std::string s = static_cast<std::ostringstream *>(
  &(std::ostringstream() << "(" << x << "," << y << "," << z << ")"))->str();

© Stack Overflow or respective owner

Related posts about c++