Extracting bool from istream in a templated function
- by Thomas Matthews
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 locales?
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.