[英]What does operator <- do in c++?
Reading really long functions can be very fun! 阅读很长的函数可能会很有趣!
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;
}
Looking at foo definition 看foo的定义
struct foo
{
int x;
foo(int a) : x(a) {}
operator int() const
{
return x;
}
};
This compiles just fine and produces the desired output. 这样可以很好地编译并产生所需的输出。 What is the mechanism that allows this?
允许这种情况的机制是什么?
It's simply, "less than negative X". 简单来说就是“小于负X”。
if (b < -x)
After 1,000 lines I'd be kinda cross-eyed, too! 一千行之后,我也会有点cross目结舌!
Arguably implicit conversion operators in c++ are controversial, eg Are implicit conversions good or bad in modern C++? 可以说C ++中的隐式转换运算符是有争议的,例如, 隐式转换在现代C ++中是好是坏? .
。 When they are defined there can be very interesting syntactical situations to the unaware programmer.
当定义它们时,对于不认识的程序员可能会有非常有趣的句法情况。 The above is just an example.
以上仅是示例。
Here is how this works: 这是这样的:
Struct foo has a conversion operator defined and therefore it is convertible to int which causes it to be implicitly converted and compared to the local variable x for less than minus x. 结构foo定义了一个转换运算符,因此可以转换为int,从而导致它被隐式转换并与局部变量x进行比较,且小于负x。
The code is actually: 该代码实际上是:
if(int(b)<(-x))
ie There is no left arrow operator <- in c++ . 即c ++中没有左箭头运算符<-。
What does operator
<-
do in c++?运算符
<-
在C ++中做什么?
It's actually not an operator (in contrast to ->
, which is one). 它实际上不是运算符(与
->
相反,后者是一个)。
As mentioned in the other answers, there are in fact two operator functions applied: 如其他答案中所述,实际上应用了两个运算符功能:
operator-()
applying a negative sign to the value operator-()
对值应用负号 operator<()
operator<()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.