Why use this comma in this return expression?

I understand what this C ++ function does, but I don’t understand why the instruction is returnwritten as follows:

int intDivide(int num, int denom){
  return assert(denom!=0), num/denom;
}

There is only one statement here, because there is only one ;, but the comma confuses me. Why not write:

int intDivide(int num, int denom){
  assert(denom!=0);
  return num/denom;
}

Besides “elegance,” is there anything you can get in the first version?

What exactly does a comma do? Does he break one operator into 2 parts, so that essentially the above 2 versions are identical?

+4
source share
3 answers

From the C ++ standard:

5.19 Comma operator [expr.comma]
1 Group operators from left to right.

expression:  
     assignment-expression  
     expression , assignment-expression  

, , ; ( 5). 87 , , . - ; , , , glvalue . (12.2), - .

, , , , @StoryTeller.

0

, , constexpr, ++ 11 constexpr , , return. , , . ++ 14 .

, , -

#define INT_DIVIDE(nom,denom) (assert(denom != 0), nom/denom)

. . . , . , , - , .

. , : void , , . :

template <typename... T>
void g(T const& arg) {
    std::initializer_list<bool>{ (f(arg), true)... };
}
+5

" ", .

Basically e1, e2means evaluation e1, and then evaluation e2- and the entire operator is the result e2. This is a short and confusing (in my opinion) way of writing what you offer. Maybe the writer is cheap on code lines.

+1
source

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


All Articles