I've seen some answers to other boost::lexical_cast
questions that assert the following is possible:
我已经看到了其他boost的一些答案::lexical_cast问题,断言以下内容是可能的:
bool b = boost::lexical_cast< bool >("true");
This doesn't work for me with g++ 4.4.3 boost 1.43. (Maybe it's true that it works on a platform where std::boolalpha is set by default)
这对我来说不适用g++ 4.4.3 boost 1.43。(也许它在std: boolalpha默认设置的平台上运行)
This is a nice solution to the string to bool problem but it lacks input validation that boost::lexical_cast provides.
这是对bool问题的字符串的一个很好的解决方案,但是它缺少boost::lexical_cast提供的输入验证。
3 个解决方案
#1
15
I'm posting the answer to my own question here for others who may be looking for something like this:
我把这个问题的答案发到我自己的问题上,给那些可能正在寻找这样的东西的人:
struct LocaleBool {
bool data;
LocaleBool() {}
LocaleBool( bool data ) : data(data) {}
operator bool() const { return data; }
friend std::ostream & operator << ( std::ostream &out, LocaleBool b ) {
out << std::boolalpha << b.data;
return out;
}
friend std::istream & operator >> ( std::istream &in, LocaleBool &b ) {
in >> std::boolalpha >> b.data;
return in;
}
};
usage:
用法:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include "LocaleBool.hpp"
int main() {
bool b = boost::lexical_cast< LocaleBool >("true");
std::cout << std::boolalpha << b << std::endl;
std::string txt = boost::lexical_cast< std::string >( LocaleBool( b ) );
std::cout << txt << std::endl;
return 0;
}
#2
11
In addition to the answer form poindexter, you can wrap the method from here in a specialized version of boost::lexical_cast
:
除了答案表格poindexter,你还可以在这里用专门的boost::lexical_cast来包装方法:
namespace boost {
template<>
bool lexical_cast<bool, std::string>(const std::string& arg) {
std::istringstream ss(arg);
bool b;
ss >> std::boolalpha >> b;
return b;
}
template<>
std::string lexical_cast<std::string, bool>(const bool& b) {
std::ostringstream ss;
ss << std::boolalpha << b;
return ss.str();
}
}
And use it:
并使用它:
#include <iostream>
#include <boost/lexical_cast.hpp>
//... specializations
int main() {
bool b = boost::lexical_cast<bool>(std::string("true"));
std::cout << std::boolalpha << b << std::endl;
std::string txt = boost::lexical_cast< std::string >(b);
std::cout << txt << std::endl;
return 0;
}
I personally liked this approach because it hides any special code (e.g. using LocaleBool
or to_bool(...)
from the link) for converting to/from bools.
我个人喜欢这种方法,因为它隐藏了任何特殊的代码(例如,使用LocaleBool或to_bool(…)从链接)转换到bools。
#3
0
Put together your own template on top of boost lexical cast for parsing. Note the "default" parameter in the example to ensure overloading works correctly (feel free to use another means if you want).
将您自己的模板放在boost词汇表之上进行解析。请注意示例中的“默认”参数,以确保重载工作正确(如果需要,可以随意使用另一种方法)。
template<typename T>
T Parse(const std::string& valStr, const T& default=T()) {
T result = boost::lexical_cast<T>(valStr);
}
Then, you can specialize for ANYTHING, including bools:
然后,你可以专攻任何东西,包括bools:
template<>
bool Parse(const std::string& valStr, const bool& default=true) {
if(strcmp(valStr.c_str(), "true") == 0) {
return true;
}
return false;
}
Obviously there are a number of ways to do this, and you can add more conditions for true vs false (I'd make sure all variants of "TRUE" and "FALSE" like "True", plus "T" and "F" work right). You could even extend it to numeric parsing.
显然有很多方法可以做到这一点,你可以为true vs false添加更多的条件(我将确保所有的“true”和“false”的变体如“true”,加上“T”和“F”)。您甚至可以将其扩展到数值解析。
#1
15
I'm posting the answer to my own question here for others who may be looking for something like this:
我把这个问题的答案发到我自己的问题上,给那些可能正在寻找这样的东西的人:
struct LocaleBool {
bool data;
LocaleBool() {}
LocaleBool( bool data ) : data(data) {}
operator bool() const { return data; }
friend std::ostream & operator << ( std::ostream &out, LocaleBool b ) {
out << std::boolalpha << b.data;
return out;
}
friend std::istream & operator >> ( std::istream &in, LocaleBool &b ) {
in >> std::boolalpha >> b.data;
return in;
}
};
usage:
用法:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include "LocaleBool.hpp"
int main() {
bool b = boost::lexical_cast< LocaleBool >("true");
std::cout << std::boolalpha << b << std::endl;
std::string txt = boost::lexical_cast< std::string >( LocaleBool( b ) );
std::cout << txt << std::endl;
return 0;
}
#2
11
In addition to the answer form poindexter, you can wrap the method from here in a specialized version of boost::lexical_cast
:
除了答案表格poindexter,你还可以在这里用专门的boost::lexical_cast来包装方法:
namespace boost {
template<>
bool lexical_cast<bool, std::string>(const std::string& arg) {
std::istringstream ss(arg);
bool b;
ss >> std::boolalpha >> b;
return b;
}
template<>
std::string lexical_cast<std::string, bool>(const bool& b) {
std::ostringstream ss;
ss << std::boolalpha << b;
return ss.str();
}
}
And use it:
并使用它:
#include <iostream>
#include <boost/lexical_cast.hpp>
//... specializations
int main() {
bool b = boost::lexical_cast<bool>(std::string("true"));
std::cout << std::boolalpha << b << std::endl;
std::string txt = boost::lexical_cast< std::string >(b);
std::cout << txt << std::endl;
return 0;
}
I personally liked this approach because it hides any special code (e.g. using LocaleBool
or to_bool(...)
from the link) for converting to/from bools.
我个人喜欢这种方法,因为它隐藏了任何特殊的代码(例如,使用LocaleBool或to_bool(…)从链接)转换到bools。
#3
0
Put together your own template on top of boost lexical cast for parsing. Note the "default" parameter in the example to ensure overloading works correctly (feel free to use another means if you want).
将您自己的模板放在boost词汇表之上进行解析。请注意示例中的“默认”参数,以确保重载工作正确(如果需要,可以随意使用另一种方法)。
template<typename T>
T Parse(const std::string& valStr, const T& default=T()) {
T result = boost::lexical_cast<T>(valStr);
}
Then, you can specialize for ANYTHING, including bools:
然后,你可以专攻任何东西,包括bools:
template<>
bool Parse(const std::string& valStr, const bool& default=true) {
if(strcmp(valStr.c_str(), "true") == 0) {
return true;
}
return false;
}
Obviously there are a number of ways to do this, and you can add more conditions for true vs false (I'd make sure all variants of "TRUE" and "FALSE" like "True", plus "T" and "F" work right). You could even extend it to numeric parsing.
显然有很多方法可以做到这一点,你可以为true vs false添加更多的条件(我将确保所有的“true”和“false”的变体如“true”,加上“T”和“F”)。您甚至可以将其扩展到数值解析。