为什么性病::字符串不是隐式转换为bool [英] why std::string is not implicitly converted to bool

查看:103
本文介绍了为什么性病::字符串不是隐式转换为bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个原因,在C ++ 的std ::字符串不是隐式转换为bool?例如:

Is there a reason why in c++ std::string is not implicitly converted to bool? For example

std::string s = ""
if (s) { /* s in not empty */ }

在其他语言(例如Python)的。我认为这是乏味的使用方法。

推荐答案

这大概可以的可现在又说,C ++ 11增加显式转换和上下文转换的概念。

This probably could be added now that C++11 has added the concepts of explicit conversions and contextual conversion.

的std ::字符串的设计,这些都不是present虽然。一个支持转化为布尔相当困难,使得类,来保持安全。特别是,转化率(和会)的地段,你几乎从来没有它希望的情况下发生的。例如,如果我们假设的std ::字符串转换成如果空,否则真正,那么你可以使用字符串基本随时随地的整数或指针之意。

When std::string was designed, neither of these was present though. That made classes that supported conversion to bool fairly difficult to keep safe. In particular, that conversion could (and would) happen in lots of cases you almost never wanted it to. For example, if we assume std::string converts to false if empty and otherwise to true, then you could use a string essentially anywhere an integer or pointer was intended.

而不是告诉你有关类型不匹配,编译器会串为bool,然后将布尔转换为整数。(假 - > 0,真的 - > 1)

Rather than telling you about the type mismatch, the compiler would convert the string to bool, and then the bool to an integer (false -> 0, true -> 1).

像这样的事情经常发生足以与字符串类型很多早期的尝试(并有的),该委员会显然决定最好还是保持隐式转换为绝对最小值(所以大约只通过字符串支持隐式转换是创建一个C风格的字符串,字符串对象)。

Things like this happened often enough with many early attempts at string types (and there were many) that the committee apparently decided it was better to keep implicit conversions to an absolute minimum (so about the only implicit conversion supported by string is to create a string object from a C-style string).

有若干的设计用于处理转换​​到更安全地bool的方法。一个是转换为无效* 来代替,其中prevented一些问题,但不是别人(这是用于输入输出流)。还有一个安全BOOL成语(实际上,更像是一个安全布尔的主题,其中有几个变化)。虽然在什么转换会和不会允许这些无疑改善了控制,其中大部分涉及的开销相当数量的(一个典型的安全需要布尔基类的〜code的50行,再加上推导从基类等)

There were a number of methods devised for handling conversion to bool more safely. One was converting to void * instead, which prevented some problems, but not others (this was used by iostreams). There was also a "safe bool" idiom (actually, more like a "safe bool" theme, of which there were several variations). While these certainly improved control over what conversions would and wouldn't be allowed, most of them involved a fair amount of overhead (a typical safe bool required a base class of ~50 lines of code, plus derivation from that base class, etc.)

至于如何显式转换和上下文转换会有所帮助,其基本思路是pretty简单。你可以(用C ++ 11开始)标记转换功能为明确,这使得它只有在显式转换为目标类型是用来使用:

As to how explicit conversion and contextual conversion would help, the basic idea is pretty simple. You can (starting with C++11) mark a conversion function as explicit, which allows it to be used only where an explicit cast to the target type is used:

struct X {
    explicit operator bool() { return true; }
};

int main() { 
    X x;
    bool b1 = static_cast<bool>(x); // compiles
    bool b2 = x;   // won't compile
}

上下文转换增添了些许让转换到bool隐含发生,但是的只有的的东西像如果语句,所以使用类与上面的转换功能,你会得到:

Contextual conversion adds a little to let the conversion to bool happen implicitly, but only in something like an if statement, so using a class with the conversion function above, you'd get:

X x;
if (x) // allowed

int y = x; // would require explicit cast to compile

我想补充一点,关于正交的投诉显得相当不适用在这里。虽然的方便的,将字符串转换为布尔并没有真正有很大的意义。如果有什么事情,我们应该抱怨多么奇怪它是字符串(0)转换为 1 (以语言在这里出现这种情况)。

I'd add that complaints about "orthogonality" seem quite inapplicable here. Although convenient, converting a string to a Boolean doesn't really make a lot of sense. If anything, we should complain about how strange it is for string("0") to convert to 1 (in languages where that happens).

这篇关于为什么性病::字符串不是隐式转换为bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆