I stumbled upon the ability to do this.
#include <iostream>
using namespace std;
int main() {
if ( ({int i = 1024; i == 10;}) ) {
cout << "In" << endl;
}
}
The important disassembly area seems to be:
-> 0x10000118f <+15>: movl $0x400, -0x18(%rbp) ; imm = 0x400
0x100001196 <+22>: cmpl $0xa, -0x18(%rbp)
0x10000119a <+26>: sete %al
0x10000119d <+29>: andb $0x1, %al
0x10000119f <+31>: movb %al, -0x19(%rbp)
0x1000011a2 <+34>: testb $0x1, -0x19(%rbp)
0x1000011a6 <+38>: je 0x1000011d9 ; <+89> at main.cpp:37
From examining this, it does seem like it takes the last statement (the comparison i == 10) as the boolean for the if statement.
I understand that this case doesn't allow me to use variable i within the if statement because of the scope operator, but wanted to know why the if statement decides to use i == 10 as the boolean statement.
For alternatives to this, I understand that a function call might be cleaner which returns a boolean that I can use to set to a variable for the if statement. However, I see MACROs that expand to this very similar style within glibc source code.
Is it an old style of programming with MACROs?
Is there a benefit to this I am missing?
Aucun commentaire:
Enregistrer un commentaire