Decorators In C

Turns out you can also do decorators in C, this came in handy for something else I had to work on recently. It’s not quite as nice, because functions aren’t first-class, but handy nonetheless.

Here’s what that looks like:

The output of which is:

vilimpoc@funky:~$ ./c-decorators
hello, C decorators!
functionOne: 1, 2
decorator precondition!
functionOne: 1, 2
decorator precondition!
functionTwo: 1, 2
decorator precondition!
functionThree: 1, 2
vilimpoc@funky:~$

This might be a bit simplistic, in reality, you’d probably want to decorate a function with a signature like the following, so that you can just change the structure and not bother w/a bajillion function declarations:

Again, not super elegant, but handy.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.