The use of comma operators and conditional operators in C++ programming

  • 2020-05-07 20:10:44
  • OfStack

comma operator:,
allows you to group two statements, one of which is expected.


expression , expression

note
The comma operator has left-to-right associations. Two comma-separated expressions are evaluated from left to right. The left operand is always counted, and all side effects are done before the right operand is calculated.
In some contexts, such as a list of function arguments, a comma can be used as a separator. Do not confuse the use of this comma as a separator with its use as an operator; The two usages are quite different.
Considered expression


e1 , e2

The type and value of this expression are the type and value of e2; The calculation results of e1 will be discarded. If the right operand is left, the result is left.
In schemes where commas are commonly used as delimiters (for example, in arguments for functions or aggregate initializers), the comma operator and its operands must be enclosed in parentheses. Such as:


func_one( x, y + 2, z );
func_two( (x--, y + 2), z );

In the above function call to func_one, three comma-separated arguments are passed: x, y + 2, and z. In a function call to func_two, the parentheses force the compiler to interpret the first comma as a sequential evaluation operator. This function call passes two arguments to func_two. The first parameter is the result of sequential computation (x--, y + 2), with the value and type of the expression y + 2; The second parameter is z.
The sample


 // cpp_comma_operator.cpp
#include <stdio.h>
int main () {
 int i = 10, b = 20, c= 30;
 i = b, c;
 printf("%i\n", i);

 i = (b, c);
 printf("%i\n", i);
}


20
30

conditional operator: ? :
grammar


expression ? expression : expression

note
Conditional operator (? :) is a three - bit operator (with three operands). The conditional operator runs as follows:
The first operand is implicitly converted to bool. Calculate the operand and complete all side effects before continuing.

If the result of the first operand is true (1), the second operand is calculated. If the result of the first operand is false (0), the third operand is calculated. The result of a conditional operator is the result of an operand (whether the second or third). Only 1 of the last two operands is evaluated in the conditional expression. Conditional expressions have right-to-left relevance. The first operand must be an integer or pointer type. The following rules apply to the second and third operands: If both operands are of the same type, the result is the same type. If both operands are arithmetic or enumeration types, a common arithmetic transform (described in the arithmetic transform) is performed to convert them to a common type. If both operands are pointer types, or if one is a pointer type and the other is a constant expression that evaluates to 0, a pointer conversion is performed to convert them to a generic type. If both operands are reference types, a reference cast is performed to convert them to a generic type. If both operands are of type void, the generic type is of type void. If both operands are the same user-defined type, the generic type is the same type. If the operands are of different types and at least one of the operands is a user-defined type, language rules are used to determine the generic type. (see the warning below.)

Any combination of the second and third operands not in the previous list is illegal. The result type is a generic type, which is lvalued if the second and third operands are of the same type and both are lvalued.

System_CAPS_warning warning
If the type of the second and third operands is different, the complex type conversion rule is invoked as specified in the C++ standard. These transformations can cause unexpected behavior, including constructing and destructing temporary objects. To this end, we strongly recommend that :(1) avoid using user-defined types as operands with conditional operators; (2) if you do want to use a user-defined type, be sure to explicitly convert each operand to a generic type.


// expre_Expressions_with_the_Conditional_Operator.cpp
// compile with: /EHsc
// Demonstrate conditional operator
#include <iostream>
using namespace std;
int main() {
 int i = 1, j = 2;
 cout << ( i > j ? i : j ) << " is greater." << endl;
}


Related articles: