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
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