在“玩笑”之后,我指的是非侵扰性地超越职能行为的能力。 一些实例:
- Print a log message before and/or after the function body.
- Wrap the function body in a try catch body.
- Measure duration of a function
- etc...
I have seen different implementations in various programming languages and libraries:
- Aspect Oriented Programming
- JavaScript s first class functions
- OOP decorator pattern
- WinAPI subclassing
- Ruby s
method_missing
- SWIG s
%exception
keyword which is meant to wrap all functions in a try/catch block can be (ab)used for the purpose of hooking
我的问题是:
- IMO this is such an incredibly useful feature that I wonder why it has never been implemented as a C++ language feature. Are there any reasons that prevent this from being made possible?
- What are some recommended techniques or libraries to implement this in a C++ program?