运算符< - 在c ++中做什么?

g24*_*24l 0 c++ operators

阅读真正长的功能可以非常有趣!

int crazylongfun()
{
  int x = -1;
  foo b = -42;

//... 1000 lines below

  if( b<-x)
  {
    std::printf("How did I get here there is no left arrow operator?\n");
  }

  return 0;    
}
Run Code Online (Sandbox Code Playgroud)

看着foo的定义

struct foo
{
  int  x;

  foo(int a) : x(a) {}

  operator int() const
  {
    return x;
  }
};
Run Code Online (Sandbox Code Playgroud)

这编译得很好并产生所需的输出.允许这种情况的机制是什么?

Gra*_*rks 5

简单地说,"不到负X".

if (b < -x)
Run Code Online (Sandbox Code Playgroud)

经过1000行之后,我也会有点眼睁睁!