Trying to put this into one line

Okay so I'm reading bytes from an array in memory like so:

1
2
3
4
5
int ReadInt()
{
unsigned char data[4] = { _Buffer[_Index++], _Buffer[_Index++], _Buffer[_Index++], _Buffer[_Index++] };
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}


Now this works great, but normally in C# I can do this in one line without creating a data array like this:

 
return ( _Buffer[_Index++] << 24 ) | ( _Buffer[_Index++] << 16 ) | ( _Buffer[_Index++] << 8 ) | _Buffer[_Index++];


When I try this in C++, I get unexpected results.

Any thoughts on why it does not work this way? My best guess is that it has to do with order of operations, but I tried adding some parenthesis in different places with no luck.


unsigned char data[4] = { _Buffer[_Index++], _Buffer[_Index++], _Buffer[_Index++], _Buffer[_Index++] };

This line is undefined anyways since you are accessing modifying Index multiple times in between a sequence point. The order in which function arguments are evaluated is undefined.
I'm sorry, but I'm not sure what you are talking about. It works fine I'm just trying to put it into one line.
Topic archived. No new replies allowed.