“operator bool() const”是什么意思?
例如:
operator bool() const
{
return col != 0;
}
col
是一个 int。 operator bool() const
是如何工作的?
For example:
operator bool() const
{
return col != 0;
}
col
is an int.
How does operator bool() const
work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
该形式的成员函数
是转换运算符。它们允许类类型的对象像
TypeName
类型一样使用,并且当它们是TypeName
类型时,使用转换函数将它们转换为TypeName
。在这种特殊情况下,
operator bool()
允许使用类类型的对象,就像它是bool
一样。例如,如果您有一个名为obj
的类类型的对象,则可以将其用作这将调用
操作符 bool()
,返回结果,并使用结果作为if
的条件。应该注意的是,在 C++11 之前,
operator bool()
是一个非常糟糕的主意。有关其不良原因和问题解决方案的详细说明,请参阅 "The Safe Bool Idiom。”C++11 添加了对显式转换运算符的支持。这些允许您编写一个安全的
显式运算符 bool()
,它可以正常工作,而无需跳过实现 Safe Bool Idiom 的麻烦。Member functions of the form
are conversion operators. They allow objects of the class type to be used as if they were of type
TypeName
and when they are, they are converted toTypeName
using the conversion function.In this particular case,
operator bool()
allows an object of the class type to be used as if it were abool
. For example, if you have an object of the class type namedobj
, you can use it asThis will call the
operator bool()
, return the result, and use the result as the condition of theif
.It should be noted that before C++11,
operator bool()
is A Very Bad Idea. For a detailed explanation as to why it is bad and for the solution to the problem, see "The Safe Bool Idiom."C++11 adds support for explicit conversion operators. These allow you to write a safe
explicit operator bool()
that works correctly without having to jump through the hoops of implementing the Safe Bool Idiom.我想提供更多代码以使其清楚。
I'd like to give more codes to make it clear.
定义类如何转换为布尔值,
()
后面的const
用于指示该方法不会发生变化(更改该类的成员)。您通常会使用此类运算符,如下所示:
Defines how the class is convertable to a boolean value, the
const
after the()
is used to indicate this method does not mutate (change the members of this class).You would usually use such operators as follows:
它是用户定义的
隐式
转换函数,用于将您的类转换为true
或false
。It's user-defined
implicit
conversion function to convert your class into eithertrue
orfalse
.这是到
bool
的隐式转换。即,只要允许隐式转换,您的类就可以通过调用该方法转换为bool
。It's an implicit conversion to
bool
. I.e. wherever implicit conversions are allowed, your class can be converted tobool
by calling that method.正如其他人所说,它用于类型转换,在本例中为
bool
。例如:现在我可以使用此类的对象,就好像它是布尔值一样:
As the others have said, it's for type conversion, in this case to a
bool
. For example:Now I can use an object of this class as if it's a boolean:
当我自己编写unique_ptr时,我发现了这种情况。给定
std::unique_ptr
的运算符==
:这个测试用例来自 libstdcxx:
请注意,因为
ptr
有一个显式运算符 bool() const noexcept;
,所以运算符重载解析
在这里工作得很好,例如,ptr == 0
选择如果这里没有
explicit
关键字,ptr<
ptr == 0
中的/code>会被转换为bool
,然后bool
会被转换为int
,因为bool operator==(int, int)
是内置的,而0
是int
。等待我们的是模棱两可的重载解析错误。这是一个最小、完整且可验证的示例:
gcc:
clang:
When writing my own unique_ptr, I found this case. Given
std::unique_ptr
'soperator==
:And this test case from libstdcxx:
Note because that
ptr
has aexplicit operator bool() const noexcept;
, sooperator overload resolution
works fine here, e.g.,ptr == 0
choosesIf it has no
explicit
keyword here,ptr
inptr == 0
will be converted intobool
, thenbool
will be converted intoint
, becausebool operator==(int, int)
is built-in and0
isint
. What is waiting for us is ambiguous overload resolution error.Here is a Minimal, Complete, and Verifiable example:
gcc:
clang:
std 容器的另一个常见用途是对自定义对象内的键值进行相等比较
Another common use is for std containers to do equality comparison on key values inside custom objects