C++ 我如何使用 boost::lexical_cast 和 std::boolalpha?即 boost::lexical_cast< bool >("true")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4452136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:27:29  来源:igfitidea点击:

How do I use boost::lexical_cast and std::boolalpha? i.e. boost::lexical_cast< bool >("true")

c++stringboostlexical-cast

提问by poindexter

I've seen some answers to other boost::lexical_castquestions 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 的平台)

Thisis a nice solution to the string to bool problem but it lacks input validation that boost::lexical_cast provides.

是 string to bool 问题的一个很好的解决方案,但它缺少 boost::lexical_cast 提供的输入验证。

采纳答案by poindexter

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;
}

回答by womple

In addition to the answer form poindexter, you can wrap the method from herein 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 LocaleBoolor to_bool(...)from the link) for converting to/from bools.

我个人喜欢这种方法,因为它隐藏了任何特殊代码(例如使用LocaleBoolto_bool(...)来自链接)用于转换为布尔值/从布尔值转换。

回答by Zack Yezek

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:

然后,您可以专攻任何事物,包括布尔值:

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.

显然有很多方法可以做到这一点,您可以为真与假添加更多条件(我会确保“真”和“假”的所有变体,如“真”,加上“T”和“F”正确工作)。您甚至可以将其扩展到数字解析。