简体   繁体   中英

What is the value of a variable definition operation in C++?

In C++ the variable definitions became an operation, which they weren't in C up until that point. That change was made so that you could place the loop variable definition inside the for loop, eg

for (int i = 0; i < N; i++) {
    printf("%d", i);
}

My question is what is the value of the variable definition operation, eg in which case what conditional statement will be executed in this example:

if (int i = N) {
    printf("yes");
} else {
    printf("no");
}

If the value of i after the initialization is not equal to zero then the if substetement will be executed. Otherwise the else substatement will be executed.

More precisely (the C++ Standard, 6.4 Selection statements)

4 The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool

And (4.12 Boolean conversions)

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

Consider a simple example

#include <iostream>
#include <cstring>

//...

char nickname[] = "mgn1993";

if ( char *p = std::strchr( nickname, 'm' ) ) *p = 'M';

std::cout << nickname << std::endl;

In this code fragment variable p is only needed inside the substatement of the if statement. There is no great sense to declare the variable in the outer scope.

You can use this as a shorthand for evaluating an expression, and use its return value inside the if block. eg

if (int i = calculateSomething()) {
   // do something with i
}

which is equivalent to

int i = calculateSomething();
if (i) {
   // do something with i
}

except that i 's scope is restricted to the if block

In the provided example, the output would be "yes", if N has a non-zero value (which would evaluate to a boolean true in C++). There is no real value to the given example, as you can very easily substitute the entire assignment with 'N' and achieve the same effect.

Maybe there is some strange fringe case where we very much need to both use and be able to adjust the value contained in N ONLY if N is non-zero and we simultaneously need to be absolutely assured the scope is restricted to the if-statement, but this seems an unlikely scenario.

That said, declaring a variable for instance in a for loop certainly has its advantages. For starters the scope is limited to the loop, some compilers optimize specifically for it, potentially cleaner code, etc.

The value of a variable definition is the variable itself.

int i = 0; // is 0, and is therefore false
int j = 5; // is 5, and is therefore true

The scope of a variable definition is the block it applies to.

So:

if(int i = returnSomething()) {
    // This point is reached if returnSomething() did not return 0
    // i is defined in this block and can be used.
}
// i is not defined at that point, its scope being limited to the if block above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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