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?










share|improve this question
























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















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?










share|improve this question
























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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word