I am working on some c Apis and I always have to check some variables are initialized and then clear/destroy/free them using special functions. such as allocation :
ogg_stream_state os;
ogg_stream_init(&os,ogg_page_serialno(&og));
and destroying:
ogg_stream_clear(&os);
I want to call the cleaner function automatically and not explicitly.
Using C++ Templates you can do it easily:
ARG
is the argument of your cleaner function, andRET
is the return type of that (RET
needed to avoid compiler warning.)example call:
now every where you like, just return from your function, it will call your cleaner function.