Opened 10 years ago
Closed 10 years ago
#7532 closed Bugs (wontfix)
lexical_cast fails on complex number with spaces
| Reported by: | Owned by: | Antony Polukhin | |
|---|---|---|---|
| Milestone: | To Be Determined | Component: | lexical_cast | 
| Version: | Boost 1.50.0 | Severity: | Problem | 
| Keywords: | Cc: | 
Description
Here's the test code that shows the problem:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <sstream>
#include <string>
typedef std::complex<double> Complex;
int main()
{
  std::string goodComplex("(1,2)"), badComplex("(1, 2)");
  try {
    Complex foo = boost::lexical_cast<Complex>(goodComplex);
    std::cout << "goodComplex = " << foo << "\n";
  }
  catch (boost::bad_lexical_cast const & e) {
    std::cerr << "goodComplex failed: " << e.what() << "\n";
  }
  
  try {
    Complex foo = boost::lexical_cast<Complex>(badComplex);
    std::cout << "badComplex = " << foo << "\n";
  }
  catch (boost::bad_lexical_cast const & e) {
    std::cerr  << "badComplex failed: "<< e.what() << "\n";
  }
  
  {
    /* Using a stringstream works, though. */
    Complex foo;
    std::istringstream tmpStream(badComplex);
    tmpStream >> foo;
    std::cout << "badComplex printed with stringstream = " << foo << "\n";
  }
}
On my machine, with g++ 4.1.2, I get the following result:
goodComplex = (1,2) badComplex failed: bad lexical cast: source type value could not be interpreted as target badComplex printed with stringstream = (1,2)
For some reason, lexical_cast chokes on the space after the comma, but the stringstream has no problem.
Change History (2)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
| Resolution: | → wontfix | 
|---|---|
| Status: | new → closed | 
lexical_cast resets the ios_base::skipws flag of an underlying stream object. This behaviour is what usually users want to have. It's unsafe to change it, because it would break some existing users code.
  Note:
 See   TracTickets
 for help on using tickets.
    

I verified that the bug is still present using the latest stable Boost, 1.51.0, and a newer compiler, g++4.5.4.