rdstate() after extracting a value from a stringstream (and boolean type)
up vote
1
down vote
favorite
(/edit : added 3rd issue)
To parse easily primitive types from various origins (aswell as possibly some objects overloading operators << & >>), I have created this template function:
#include <sstream>
#include <iostream> //only for the debug trace
#include <typeinfo> //only for the debug trace
template<typename T>
static inline bool getValue(std::string value, T& result)
{
//std::cout << "getValue : '" << value << "'" << std::endl;
//std::cout << "type : " << typeid(T).name() << std::endl;
std::stringstream ss(value);
ss >> std::boolalpha >> result;
//std::cout << "result : " << result << std::endl;
//std::cout << "ss.rdstate() : " << ss.rdstate() << std::endl;
return ss.good();
}
This template function takes a string, and will try to extract a value according to the given reference type.
Output :
For a boolean, with value "true" :
getValue : 'true'
type : b
result : 1
ss.rdstate() : 0
For a boolean, with value "1" :
getValue : '1'
type : b
result : 0
ss.rdstate() : 4
For an int :
getValue : '60'
type : i
result : 60
ss.rdstate() : 2
For a string, with value "blah" : (yes i know, useless for a string, but if i want to loop on several parameters with various types...)
getValue : 'blah'
type : Ss
result : blah
ss.rdstate() : 2
I have here 2 issues :
1) boolean values
To be able to read value strings "true" or "false", I had to add this "std::boolalpha" manipulator. But then I am unable to successfully parse '0' or '1' for bool type.
2) rdstate flag
In case of error when parsing (with the boolean issue for instance) , rdstate returns 4 (ie "fail").
In case, it is correct, it will either return '0' (good) for the boolean ; or '2' (ie "eof") for integers or string.
Can someone explain why this difference between these types ? should i consider "eof" as correct ? I assume yes.
Documentation of operator>> (http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/) states that failbit is set for :
"Either no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type"
So the empty string will trigger a fail. (checked and rdstate is 6, ie eof|fail);
3) side question : value in case of error :
Testing it, I tried an error case when setting an int. I called my function with a zero-initialized int, and an empty string :
int timeout=0;
getValue ("", timeout);
getValue : ''
type : i
result : 60000
ss.rdstate() : 6
This 60 000 value looks very weird to me. it corresponds to the init value of another int.. WTF !
For sure I am checking the result, but some other users may not. Should I enable the exceptions on failbits?
c++ boolean extraction istream
add a comment |
up vote
1
down vote
favorite
(/edit : added 3rd issue)
To parse easily primitive types from various origins (aswell as possibly some objects overloading operators << & >>), I have created this template function:
#include <sstream>
#include <iostream> //only for the debug trace
#include <typeinfo> //only for the debug trace
template<typename T>
static inline bool getValue(std::string value, T& result)
{
//std::cout << "getValue : '" << value << "'" << std::endl;
//std::cout << "type : " << typeid(T).name() << std::endl;
std::stringstream ss(value);
ss >> std::boolalpha >> result;
//std::cout << "result : " << result << std::endl;
//std::cout << "ss.rdstate() : " << ss.rdstate() << std::endl;
return ss.good();
}
This template function takes a string, and will try to extract a value according to the given reference type.
Output :
For a boolean, with value "true" :
getValue : 'true'
type : b
result : 1
ss.rdstate() : 0
For a boolean, with value "1" :
getValue : '1'
type : b
result : 0
ss.rdstate() : 4
For an int :
getValue : '60'
type : i
result : 60
ss.rdstate() : 2
For a string, with value "blah" : (yes i know, useless for a string, but if i want to loop on several parameters with various types...)
getValue : 'blah'
type : Ss
result : blah
ss.rdstate() : 2
I have here 2 issues :
1) boolean values
To be able to read value strings "true" or "false", I had to add this "std::boolalpha" manipulator. But then I am unable to successfully parse '0' or '1' for bool type.
2) rdstate flag
In case of error when parsing (with the boolean issue for instance) , rdstate returns 4 (ie "fail").
In case, it is correct, it will either return '0' (good) for the boolean ; or '2' (ie "eof") for integers or string.
Can someone explain why this difference between these types ? should i consider "eof" as correct ? I assume yes.
Documentation of operator>> (http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/) states that failbit is set for :
"Either no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type"
So the empty string will trigger a fail. (checked and rdstate is 6, ie eof|fail);
3) side question : value in case of error :
Testing it, I tried an error case when setting an int. I called my function with a zero-initialized int, and an empty string :
int timeout=0;
getValue ("", timeout);
getValue : ''
type : i
result : 60000
ss.rdstate() : 6
This 60 000 value looks very weird to me. it corresponds to the init value of another int.. WTF !
For sure I am checking the result, but some other users may not. Should I enable the exceptions on failbits?
c++ boolean extraction istream
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
I'd overload it instead of specialize but yes. Check if the size ofvalueis one and if it is read it in regularly and if it isn't usestd::boolalpha.
– NathanOliver
Nov 9 at 16:11
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
(/edit : added 3rd issue)
To parse easily primitive types from various origins (aswell as possibly some objects overloading operators << & >>), I have created this template function:
#include <sstream>
#include <iostream> //only for the debug trace
#include <typeinfo> //only for the debug trace
template<typename T>
static inline bool getValue(std::string value, T& result)
{
//std::cout << "getValue : '" << value << "'" << std::endl;
//std::cout << "type : " << typeid(T).name() << std::endl;
std::stringstream ss(value);
ss >> std::boolalpha >> result;
//std::cout << "result : " << result << std::endl;
//std::cout << "ss.rdstate() : " << ss.rdstate() << std::endl;
return ss.good();
}
This template function takes a string, and will try to extract a value according to the given reference type.
Output :
For a boolean, with value "true" :
getValue : 'true'
type : b
result : 1
ss.rdstate() : 0
For a boolean, with value "1" :
getValue : '1'
type : b
result : 0
ss.rdstate() : 4
For an int :
getValue : '60'
type : i
result : 60
ss.rdstate() : 2
For a string, with value "blah" : (yes i know, useless for a string, but if i want to loop on several parameters with various types...)
getValue : 'blah'
type : Ss
result : blah
ss.rdstate() : 2
I have here 2 issues :
1) boolean values
To be able to read value strings "true" or "false", I had to add this "std::boolalpha" manipulator. But then I am unable to successfully parse '0' or '1' for bool type.
2) rdstate flag
In case of error when parsing (with the boolean issue for instance) , rdstate returns 4 (ie "fail").
In case, it is correct, it will either return '0' (good) for the boolean ; or '2' (ie "eof") for integers or string.
Can someone explain why this difference between these types ? should i consider "eof" as correct ? I assume yes.
Documentation of operator>> (http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/) states that failbit is set for :
"Either no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type"
So the empty string will trigger a fail. (checked and rdstate is 6, ie eof|fail);
3) side question : value in case of error :
Testing it, I tried an error case when setting an int. I called my function with a zero-initialized int, and an empty string :
int timeout=0;
getValue ("", timeout);
getValue : ''
type : i
result : 60000
ss.rdstate() : 6
This 60 000 value looks very weird to me. it corresponds to the init value of another int.. WTF !
For sure I am checking the result, but some other users may not. Should I enable the exceptions on failbits?
c++ boolean extraction istream
(/edit : added 3rd issue)
To parse easily primitive types from various origins (aswell as possibly some objects overloading operators << & >>), I have created this template function:
#include <sstream>
#include <iostream> //only for the debug trace
#include <typeinfo> //only for the debug trace
template<typename T>
static inline bool getValue(std::string value, T& result)
{
//std::cout << "getValue : '" << value << "'" << std::endl;
//std::cout << "type : " << typeid(T).name() << std::endl;
std::stringstream ss(value);
ss >> std::boolalpha >> result;
//std::cout << "result : " << result << std::endl;
//std::cout << "ss.rdstate() : " << ss.rdstate() << std::endl;
return ss.good();
}
This template function takes a string, and will try to extract a value according to the given reference type.
Output :
For a boolean, with value "true" :
getValue : 'true'
type : b
result : 1
ss.rdstate() : 0
For a boolean, with value "1" :
getValue : '1'
type : b
result : 0
ss.rdstate() : 4
For an int :
getValue : '60'
type : i
result : 60
ss.rdstate() : 2
For a string, with value "blah" : (yes i know, useless for a string, but if i want to loop on several parameters with various types...)
getValue : 'blah'
type : Ss
result : blah
ss.rdstate() : 2
I have here 2 issues :
1) boolean values
To be able to read value strings "true" or "false", I had to add this "std::boolalpha" manipulator. But then I am unable to successfully parse '0' or '1' for bool type.
2) rdstate flag
In case of error when parsing (with the boolean issue for instance) , rdstate returns 4 (ie "fail").
In case, it is correct, it will either return '0' (good) for the boolean ; or '2' (ie "eof") for integers or string.
Can someone explain why this difference between these types ? should i consider "eof" as correct ? I assume yes.
Documentation of operator>> (http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/) states that failbit is set for :
"Either no characters were extracted, or the characters extracted could not be interpreted as a valid value of the appropriate type"
So the empty string will trigger a fail. (checked and rdstate is 6, ie eof|fail);
3) side question : value in case of error :
Testing it, I tried an error case when setting an int. I called my function with a zero-initialized int, and an empty string :
int timeout=0;
getValue ("", timeout);
getValue : ''
type : i
result : 60000
ss.rdstate() : 6
This 60 000 value looks very weird to me. it corresponds to the init value of another int.. WTF !
For sure I am checking the result, but some other users may not. Should I enable the exceptions on failbits?
c++ boolean extraction istream
c++ boolean extraction istream
edited Nov 9 at 17:05
asked Nov 9 at 15:56
Pellekrino
1168
1168
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
I'd overload it instead of specialize but yes. Check if the size ofvalueis one and if it is read it in regularly and if it isn't usestd::boolalpha.
– NathanOliver
Nov 9 at 16:11
add a comment |
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
I'd overload it instead of specialize but yes. Check if the size ofvalueis one and if it is read it in regularly and if it isn't usestd::boolalpha.
– NathanOliver
Nov 9 at 16:11
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
I'd overload it instead of specialize but yes. Check if the size of
value is one and if it is read it in regularly and if it isn't use std::boolalpha.– NathanOliver
Nov 9 at 16:11
I'd overload it instead of specialize but yes. Check if the size of
value is one and if it is read it in regularly and if it isn't use std::boolalpha.– NathanOliver
Nov 9 at 16:11
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229139%2frdstate-after-extracting-a-value-from-a-stringstream-and-boolean-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Problem 1 can be solved by adding an overload that takes bool.
– NathanOliver
Nov 9 at 15:59
you mean specializing the template for bool, and there check if '1' or if 'true' manually ?
– Pellekrino
Nov 9 at 16:10
I'd overload it instead of specialize but yes. Check if the size of
valueis one and if it is read it in regularly and if it isn't usestd::boolalpha.– NathanOliver
Nov 9 at 16:11