object.operator bool() 和 (bool) object 有什么区别?

What is the difference between object.operator bool() and (bool) object?

我有一个 class ,我已经像这样显式地重载了运算符 bool :-

class Foo {
  explicit operator bool() {
    // return_something_here
  }
};

然而,当我在 gdb 中 运行 以下两个时,我得到 :-

gdb) p fooobj.operator bool()
 = true
gdb) p (bool)(fooobj)
 = false

这两个调用有什么不同,为什么它们 return 不同?

编辑:- 我正在使用 clang 编译器。

注意:- 第二个值 (false) 是我希望使用第一种语法 returned 的正确值。我正在使用代码生成器,所以我无法完全控制生成的 c++ 以防万一有人好奇为什么我不只使用第二种语法。

即使那样,两者之间的区别仍然是一个悬而未决的问题。

我只是 运行 一些快速测试,似乎 gdb 不能很好地处理用 clang 编译的代码。这是一个测试程序:

#include <iostream>

using namespace std;

class Foo {
public:
  Foo() : m_Int(0) {}
  operator bool() {
    return true;  // also tried false here
  }
private:
  int m_Int;
};

int main()
{
  Foo f;
  if (f.operator bool()) cout << "operator bool is true.\n";

  if ((bool)f)  cout << "(bool)f is true.\n";

  return 0;
}

当二进制为 运行 时,输出符合预期,即 (bool)f 与 f.operator bool() 相同,与编译器无关。但是,如果 gdb 与使用 g++ 的代码构建一起使用,则 p 命令的行为正确。然而,当 gdb 在使用 clang++ 构建的代码上 运行 时,我得到:

(gdb) print f.operator bool()
Couldn't find method Foo::operatorbool
(gdb) 

我是 运行ning clang v. 3.4,gcc v. 4.8.4 on Ubuntu 14.04.

事实上,快速搜索显示了这一点:Is it possible to debug a gcc-compiled program using lldb, or debug a clang-compiled program using gdb?。所以,我尝试了 lldb,它按预期工作。这与我在调查时添加的评论一致。