Extracting bool from istream in a templated function
Posted
by Thomas Matthews
on Stack Overflow
See other posts from Stack Overflow
or by Thomas Matthews
Published on 2010-05-21T22:22:41Z
Indexed on
2010/05/21
22:40 UTC
Read the original article
Hit count: 484
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.
© Stack Overflow or respective owner