Loss of data when using stringstream conversion

A

abhay.burli

Hello Group,

I have a small utility function to convert from 'string' to 'int' or
'double' as below:-

// start-snip
template <class out_type, class in_type>
out_type convert(const in_type& t)
{
std::istringstream stream;
stream>>std::noskipws;
stream.str(t); // insert value to stream
// store conversion’s result here
out_type result;
stream >> result; // write value to result
return result;
}
// end-snip

So,

int num1 = convert<int>("11"); // num = 11. Correct
int num2 = convert<int>("11.345"); // num2 = 11! No failbit/badbit is
set. I want this to fail.

Is there a simple way to make the latter statement fail the conversion
using some stringstream flags. Similar functionality is available to
handle spaces using the 'noskipws' flag i.e. ' 123' would fail to
convert. I could not find any such flag within stringstream that
prevents this demotion.

Any help appreciated.

Thanks,
Abhay
 
J

Jerry Coffin

Hello Group,

I have a small utility function to convert from 'string' to 'int' or
'double' as below:-

// start-snip
template <class out_type, class in_type>
out_type convert(const in_type& t)
{
std::istringstream stream;
stream>>std::noskipws;
stream.str(t); // insert value to stream
// store conversion?s result here
out_type result;
stream >> result; // write value to result
return result;
}
// end-snip

So,

int num1 = convert<int>("11"); // num = 11. Correct
int num2 = convert<int>("11.345"); // num2 = 11! No failbit/badbit is
set. I want this to fail.

After you've done the conversion, check that the input stream is empty
-- i.e. that you've converted _all_ characters that were put into the
stream. If there are characters left that didn't convert, you have a
failure.

Since the stream you're using is purely local, setting its failbit or
badbit won't really accomplish anything though -- you need to signal
failure to the caller, such as via a return value or an exception.
Choosing between those two isn't necessarily easy though -- it'll depend
on whether a failed conversion indicates a fundamental error in logic
(which would indicate using an exception) or simply incorrect input
(which would indicate using a return value).
 
R

red floyd

Hello Group,

I have a small utility function to convert from 'string' to 'int' or
'double' as below:-

// start-snip
template <class out_type, class in_type>
out_type convert(const in_type& t)
{
        std::istringstream stream;
        stream>>std::noskipws;
        stream.str(t); // insert value to stream
        // store conversion’s result here
        out_type result;
        stream >> result; // write value to result
        return result;}

// end-snip

So,

int num1 = convert<int>("11"); // num = 11. Correct
int num2 = convert<int>("11.345"); // num2 = 11! No failbit/badbit is
set. I want this to fail.
Why should it fail? behavior of operator>>(istream& is, int& val) is
to read until the first non-digit number and set val to the integer
value.

You're going to have to parse it manually to get the behavior you
want. See strtol().
 
A

abhay.burli

After you've done the conversion, check that the input stream is empty
-- i.e. that you've converted _all_ characters that were put into the
stream. If there are characters left that didn't convert, you have a
failure.

Thanks. This works like a charm for my purposes.
Since the stream you're using is purely local, setting its failbit or
badbit won't really accomplish anything though -- you need to signal
failure to the caller, such as via a return value or an exception.
Choosing between those two isn't necessarily easy though -- it'll depend
on whether a failed conversion indicates a fundamental error in logic
(which would indicate using an exception) or simply incorrect input
(which would indicate using a return value).

Basically i am implementing my own stack-based language where i have a
'Numeric' class that can accept either an 'int' or a 'double' in its
ctor depending on user-input. i.e. "1" would be 'int' whereas "1.0"
would be 'double'. So returning an error-value like
std::numeric_limits<out-Type>::max() would do. It is not an
'exceptional' situation in my case.

Regards,
Abhay
 
A

abhay.burli

Why should it fail?  behavior of operator>>(istream& is, int& val) is
to read until the first non-digit number and set val to the integer
value.

O.K. To be specific, i did not 'intend' it to fail. I wanted to make
the conversion a failure using some tweak for my purposes.

You're going to have to parse it manually to get the behavior you
want. See strtol().

Possible, but i wanted to try it the modern way :) i.e. using
stringstreams.


Thanks,
Abhay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top