Returns a list of values ​​between brackets (10, 20, 30, 40)?

I work in C ++ (not C ++ / CLI) in Visual Studio 2012 .

I don’t understand why this code works, I would expect it to fail at compile time, but it doesn’t even work at runtime:

double MyClass::MyMethod() const
{
    //some code here        
    return (10, 20, 30, 40);
}

I made this code by mistake, it wasn’t on purpose, I noticed an error when I ran Unit tests. And I am surprised that this works. When I run it, it returns 40 , the last number in the list.

Can someone explain to me what this syntax means and why it works?

+4
source share
4 answers

, , . ++ 5.18 Comma :

, , ; ( 5). 83 , , , .

:

, for.

C- c , , .

, , , , gcc -Wall :

warning: left operand of comma operator has no effect [-Wunused-value]
  return (10, 20, 30, 40);
              ^

.

+9

" " ++, for.

, , , . - , return return (40);

+4

(10, 20, 30, 40) 4 , ,. , , - .

+1

, i.e.

return ().

, , return (/*valid evaluation*/)

Comma operator i.e 40

+1

All Articles