简体   繁体   English

加倍为真/假

[英]double as true / false

Bjarne suggests using the condition in if's as scope restriction. Bjarne建议使用if的条件作为范围限制。 In particular this example. 特别是这个例子。

if ( double d = fd()  ) {
   // d in scope here...
}

I'm curios how to interpret the declaration in a true / false sense. 我很想知道如何在对/错的意义上解释声明。

  1. It's a declaration 这是一个宣言
  2. It's a double. 这是双重的。

Edit: It's in 6.3.2.1 The C++ programming language as a recommendation. 编辑:建议使用6.3.2.1 C ++编程语言。

Edit2: templatetypedefs suggestion of pointers, in particular with dynamic casts, might give insight to Bjarnes suggestion. Edit2:templatetypedefs的指针建议,尤其是动态转换时,可能会为Bjarnes的建议提供见识。

SteveJessop tells me: - A condition is not an expression it can also be a declaration, the value used, is the value being evaluated. SteveJessop告诉我:-条件不是表达式,也可以是声明,使用的值是要求值的值。

The code that you're seeing is a specialized technique for declaring variables in if statements. 您所看到的代码是一种专用技术,用于在if语句中声明变量。 You commonly see something like this: 您通常会看到以下内容:

if (T* ptr = function()) {
    /* ptr is non-NULL, do something with it here */
} else {
    /* ptr is NULL, and moreover is out of scope and can't be used here. */
}

A particularly common case is the use of dynamic_cast here: 一个特别常见的情况是在此处使用dynamic_cast

if (Derived* dPtr = dynamic_cast<Derived*>(basePtr)) {
     /* basePtr really points at a Derived, so use dPtr as a pointer to it. */
} else {
     /* basePtr doesn't point at a Derived, but we can't use dPtr here anyway. */
}

What's happening in your case is that you're declaring a double inside the if statement. 您的情况是在if语句中声明了一个double C++ automatically interprets any nonzero value as true and any zero value as false . C ++自动将任何非零值解释为true并将任何零值解释为false What this code means is "declare d and set it equal to fd() . If it is nonzero, then execute the if statement." 该代码的意思是“声明d并将其设置为等于fd() 。如果它不为零,则执行if语句。”

That said, this is a Very Bad Idea because double s are subject to all sorts of rounding errors that prevent them from being 0 in most cases. 就是说,这是一个非常糟糕的主意,因为double会受到各种舍入错误的影响,在大多数情况下,防止它们变为0。 This code will almost certainly execute the body of the if statement unless function is very well-behaved. 除非function非常完善,否则这段代码几乎可以肯定会执行if语句的主体。

Hope this helps! 希望这可以帮助!

In the example Stroustrup gives, the code in the if block divides a value by d : 在Stroustrup给出的示例中, if块中的代码将值除以d

if (double d = prim(true)) {
    left /= d;
    break;
}

Division by 0 is undefined behavior, so it makes some sense in this case to test d against the value 0.0 before dividing. 除以0是未定义的行为,因此在这种情况下 ,在除之前先将d与值0.0进行测试是有意义的。 Putting the definition in the condition is a convenient way to do this, for the reasons Stroustrup states. 由于Stroustrup指出的原因,将定义置于条件中是一种方便的方法。

Your code gives no reason why the value 0.0 would be special, and therefore it is not clear why anyone would combine the definition of d with that test. 您的代码没有给出为什么值0.0会是特殊的原因,因此不清楚为什么有人会将d的定义与该测试结合在一起。 Only use Stroustrup's pattern when "false" values of the type you're defining need to be treated specially. 仅在需要特别对待要定义的类型的“假”值时,才使用Stroustrup的模式。 Otherwise just do this: 否则,只需执行以下操作:

{
    double d = fd();
    // d in scope here...
}

the if statement predicates on the value that is assigned to the variable in the assignment expression. if语句基于在赋值表达式中分配给变量的值。 If the double evaluates to anything but 0.0 it will run the code inside. 如果double计算除0.0以外的任何值,它将在其中运行代码。

Note that you are not supposed to compare doubles with zero, but it generally works in my experience. 请注意,您不应该将双精度数与零进行比较,但是根据我的经验,它通常是有效的。

Basically, you shouldn't do this. 基本上,您不应该这样做。

The other contributors to this topic have found that this expression is used to exclude the zero case so as to avoid a divide-by-zero. 该主题的其他贡献者还发现,此表达式用于排除零的情况,以避免被零除。 That's definitely smart and such a situation legitimizes this usage as far as I'm concerned (but do consider the confusion such code may cause). 这绝对是明智的,就我而言,这种情况使这种用法合法化(但请考虑这样的代码可能引起的混乱)。

It is both a declaration and a double. 它既是一个声明又是一个双重声明。 This is quite equivalent to 这相当于

{
    double d = fd();
    if (d) {
    }
}

However, this pattern is worth the small additional syntax to simplify, as it is fairly useful and common. 但是,此模式值得使用一些小的附加语法来简化,因为它相当有用且通用。 In addition, the transformation is less obvious once you start adding else clauses, as d is out of scope for them. 另外,一旦开始添加else子句,转换就不太明显了,因为d在它们的范围之外。

Also, as others have noted, it's useful in general but FP types in specific have some issues when compared against 0. 另外,正如其他人指出的那样,它通常很有用,但是与0相比,特定的FP类型存在一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM