运算符优先级(bool 之前的 void*?)

发布于 2024-10-04 23:25:57 字数 1127 浏览 4 评论 0原文

在回答这个问题时,我做了一些研究,这确实让我感到困惑。

我注意到成功打开的两个 ifstream 不相等,但失败的两个 ifstream 相等。 首先我查看了 cplusplus.com。 operator ! 返回badbit 和failbit。我认为与此相反的仍然是返回这两个位的状态,但是翻转了。

  • 错误,因为两次成功的调用并不相等。

然后我认为这是一个会返回某些内容的 operator bool 。因此,我尝试从 ifstream 回溯并找到 istream::operator bool(),它返回 _Ok

  • 但是仍然错误,这似乎根本没有被调用(也不可能被调用,因为两个成功的调用仍然不相等)。

所以我改变了我的方法并检查了 Visual Studio 中的反汇编。我发现了什么?
if (file0 != file1) { 不会调用运算符 bool()而是调用运算符 void* () (或者实际上是__imp_std::ios_base::operator void *)。

所以我的问题是..

  • 在尝试将其转换为指针值之前,是否不应该调用任何找到的运算符 bool () ?
  • 是不是我错过了一些 operator bool() ,而它又调用了 operator void*
  • 这是一些我不理解的优化吗?

  • 或者我完全错了,C++ 实际上认为在此比较中 void*bool 更好匹配?

When answering this question I made some research which really confuses me.

I noticed that two ifstreams that succesfully open are not equal but two ifstreams that fail are.
At first i checked cplusplus.com. The operator ! returns the status of the badbit and failbit. I think that the opposite of this would still be to return the status of these two bits, but flipped.

  • Wrong, because two succesful calls are not equal.

So then I figured it was an operator bool somewhere that would return something. So I tried to backtrack from ifstream and found the istream::operator bool(), which is returning _Ok.

  • Still wrong however, this doesn't seem to be called at all (and couldn't be, since the two successful calls are still not equal).

So I changed my approach and checked the disassembly from Visual Studio. And what do I find?
if (file0 != file1) { doesn't call the operator bool(), but rather the operator void* () (or really __imp_std::ios_base::operator void *).

So the questions I have are..

  • Shouldn't any operator bool () found be called before trying to casting it to pointer values?
  • Is it some operator bool() I missed that in turn is calling the operator void* ?
  • Is this some optimizing that I don't understand?

  • Or am I completely wrong in that C++ actually thinks that void* is a better match than bool in this comparison?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2024-10-11 23:25:57

1.) 不能多次重载但返回类型不同。
2.) 是的,operator! 返回 badbit/failbit,但是 operator!operator!= 完全不同,后者是你正在使用。

1.) You cannot overload multiple times but with different return types.
2.) Yes, operator! returns the badbit/failbit, but operator! is something entirely different from operator!=, which is the one you are using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文