Why does std::stof not throw when passed an argument it cannot convert?

I'm working on a project where I want to accept an input of the form {float}{single-letter-identifier}, for example 15.6E, or 10W.

To do this, I thought that I could take the input string, strip off the last letter and then check to see if a conversion could be performed to float, using std::stof. This would be nested in a try-catch block, and allow me to notify the user of invalid input.

The open-standard of the STL here (page 653) states that std::stof throws:

invalid_argument if wcstod or wcstold reports that no conversion could be performed.

However, it doesn't throw when passed something it cannot convert, e.g. "48East". A code sample to reproduce this behavior is below:

std::wstring szString = L"48East";
try{
     float f = std::stof(szString);
} catch( ... )
{
     std::cout << "test" << std::endl;
}

This is compiled on MSVC10 in debug mode with /Od, so I'm assuming the call is not optimized away.

I'd appreciate any help (or guidance as to where I've misunderstood/misread the spec!).

Answers


As I read it, stof converts as much of the input string as it can until it finds something it can't convert. if it can't convert anything it throws invalid_argument.


The MSVC implementation parses the valid characters from the beginning and converts them successfully so your result is 48F in the above case. If instead you had "East48" you would get the invalid argument exception as expected.

Edit: Furthermore if you pass the optional index out parameter the function will report the index of the insignificant characters not used for the conversion. In the above example this would allow you to do something like this:

std::wstring szString = L"48East";
try{
     size_t i(0);
     float f = std::stof(szString, &i);
     std::cout << f << szString.substr(i).c_str() << std::endl;
} catch( ... )
{
     std::cout << L"test" << std::endl;
}

which produces the output 48East


Need Your Help

Ruby gem installation problem on Mac which is behind firewall

ruby proxy gem

Please provide me solution for the problem of,

Check if all elements in list1 exist in list2 python

python list python-3.x subset

I want to be able to find whether all elements in list1 exist in list2, both lists containing chars. The number of times the same element appears in each list is important.