Double as true / false

Bjarne suggests using the condition if as a limitation on volume. In particular, this example.

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

I am curious how to interpret the ad in the true / false sense.

  • This ad
  • It is double.

Edit: This is in 6.3.2.1 the C ++ programming language as a recommendation.

Edit2: a suggestion of templatetypedefs of pointers, in particular with dynamic clicks, can give an idea of ​​the Bjarns proposal.

SteveJessop tells me: - The condition is not an expression, which can also be a declaration, the value used is the evaluated value.

+6
source share
4 answers

The code you see is a specialized method for declaring variables in if . You usually 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 to use dynamic_cast here:

 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 happens in your case is that you declare a double inside the if . C ++ automatically interprets any non-zero value as true and any zero value as false . This code means "declare d and set it to fd() . If it is nonzero, execute the if ."

However, this is a very bad idea, because double are subject to all kinds of rounding errors, which in most cases prevent them from being 0. This code will almost certainly execute the body of the if if the function does not behave very well.

Hope this helps!

+6
source

In the example provided by Stroustrup, the code in the if block divides the value by d :

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

Division by 0 is undefined behavior, so in this case it makes sense to check d for a value of 0.0 before division. Including a definition in a condition is a convenient way to do this, for the reasons that Stroustrup points out.

There is no reason in your code why a value of 0.0 would be special, and therefore it is not clear why anyone would combine the definition of d with this test. Use only the Stroustrup pattern when you need to "process" the values ​​of the "false" type that you define. Otherwise, just do the following:

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

The if predicts the value assigned to the variable in the assignment expression. If double evaluates to anything other than 0.0, it will run the code inside.

Please note that you should not compare doubles with zero, but as a rule, this works in my experience.

Basically, you should not do this.

Other participants in this topic have found that this expression is used to exclude the null case in order to avoid dividing by zero. This is definitely smart and this situation legitimizes this use, as far as I know (but think about the confusion that such code can cause).

+2
source

This is both a declaration and a double one. It is quite equivalent

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

However, this template costs a little extra syntax to simplify, as it is quite useful and common. Also, the conversion is less obvious after you start adding else clauses, since d not available to them.

In addition, as others have noted, it is useful in general, but FP types in a specific have some problems when compared to 0.

+2
source

Source: https://habr.com/ru/post/919078/


All Articles