开发者

Extracting bool from istream in a templated function

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜