我正在将我的字段类读取函数转换为一个模板函数。我有 int、unsigned int、long
和 unsigned long
的字段类。这些都使用相同的方法从 istringstream
中提取值(只是类型发生了变化):
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;
}
棘手的部分是 bool
( boolean )类型。 boolean 值的文本表示有很多:
0、1、T、F、TRUE、FALSE
、和所有不区分大小写的组合
问题如下:
- C++ 标准说的是什么
用于提取
bool
的有效数据, 使用流提取 接线员? - 因为 boolean 值可以表示为
文本,这是否涉及
区域设置
? - 是否依赖于此平台?
我想通过不为 bool
输入编写自己的处理程序来简化我的代码。
我使用的是 MS Visual Studio 2008(版本 9)、C++、Windows XP 和 Vista。
最佳答案
true 和 false 的字符串由 std::numpunct::truename()
定义和 std::numpunct::falsename()
.你可以获得 numpunct
对于带有 use_facet <numpunct <char> >(stream.getloc())
的给定流,如果我正确理解文档的话。
编辑:您可以切换是否使用 "1"
/"0"
或 "true"
/"false
与 std::noboolalpha
和 std::boolalpha
.
关于c++ - 在模板函数中从 istream 中提取 bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2885991/