Possible Du开发者_运维知识库plicate:
C++ Comma Operator
About a year ago I noted some obscure syntax in a coding project I was working on:
table_value = table_index += 2, valueFromTable(table_index);
Does anyone recognise this?, it's like an assignment with an additional statement. This compiled in our entire suite of cross-platform compilers, so I'm pretty certain its valid C++ but I've never seen anything like it.
Any insight would be appreciated.
Gearoid
EDIT: heres some working code:
#include <iostream>
using namespace std ;
int valueFromTable(int a) { return a ; }
int main()
{
int table_index = 0 ;
int table_value = table_index += 2, valueFromTable(12);
cout<<table_value<<endl;
return 0 ;
}
This is the Comma operator.
It's standard C and C++ but heavily frowned upon.
It evaluates both arguments, and returns the result of the second.
精彩评论