I'm converting my fields class read functions into one template function. I have field classes for int, unsigned int, long,
and unsigned long
. These all use the same method for extracting a value from an istringstream
(only the types change):
template <typename Value_Type>
Value_Type Extract_Value(const std::string& input_string)
{
std::istringstream m_string_stream;
m_string_stream.str(input_string);
m_string_stream.开发者_如何转开发clear();
m_string_stream >> value;
return;
}
The tricky part is with the bool
(Boolean) type. There are many textual representations for Boolean:
0, 1, T, F, TRUE, FALSE
, and all the case insensitive combinations
Here's the questions:
- What does the C++ standard say are
valid data to extract a
bool
, using the stream extraction operator? - Since Boolean can be represented by
text, does this involve
locale
s? - Is this platform dependent?
I would like to simplify my code by not writing my own handler for bool
input.
I am using MS Visual Studio 2008 (version 9), C++, and Windows XP and Vista.
The strings for true and false are defined by std::numpunct::truename()
and std::numpunct::falsename()
. You can get the numpunct
for a given stream with use_facet <numpunct <char> >(stream.getloc())
, if I understand the documentation correctly.
EDIT: You can toggle whether to use "1"
/"0"
or "true"
/"false
with std::noboolalpha
and std::boolalpha
.
精彩评论