C ++ has a nice function that allows you to call some code at the end of a block - a destructor. Yesterday, I accidentally discovered a trick that works in C similar to a destructor in C ++. For illustration, this trick is used to automatically close a file:
#include <stdio.h>
#define AUTO_FILE( id, name, access ) \
for ( FILE* id = fopen( name, access ); id != NULL; fclose( id ), id = NULL )
int main()
{
AUTO_FILE( f, "hello.txt", "w" ) {
fprintf( f, "Hello World!" );
}
return 0;
}
I think this is good and can have interesting customs. For example, if you combine it with a stack data structure, you can declare very intuitive and readable tree structures directly in the source code:
frame("frm") {
menu("mnu") {
popupmenu("file", "File") {
menuitem("exit", "Exit");
}
popupmenu("help", "Help") {
menuitem("about", "About...");
}
}
label("lbl", "Hello World!");
button("btn", "Push me!", btn_onclick);
}
, , , . break return , fclose . continue, , break ++.:-) , , , .
, :
C?