views:

44

answers:

1

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:

  1. What does the C++ standard say are valid data to extract a bool, using the stream extraction operator?
  2. Since Boolean can be represented by text, does this involve locales?
  3. 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.

+1  A: 

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.

MSN
Is this case insensitive? Is this in a specific version / revision of the C++ language?
Thomas Matthews
Since the default values are "true" and "false", I'm going to assume that it's case sensitive. Also, I'm going to assume it's been in the standard for a while.
MSN