Function hooking in C++?
<p>With "hooking" I mean the ability to non-intrusively override the behavior of a function. Some examples:</p>

<ul>
<li>Print a log message before and/or after the function body.</li>
<li>Wrap the function body in a try catch body.</li>
<li>Measure duration of a function</li>
<li>etc...</li>
</ul>

<p>I have seen different implementations in various programming languages and libraries:</p>

<ul>
<li>Aspect Oriented Programming</li>
<li>JavaScript's first class functions</li>
<li>OOP decorator pattern</li>
<li>WinAPI subclassing</li>
<li>Ruby's <code>method_missing</code></li>
<li><a href="http://www.swig.org/" rel="nofollow">SWIG</a>'s <code>%exception</code> keyword which is meant to wrap all functions in a try/catch block can be (ab)used for the purpose of hooking</li>
</ul>

<p>My questions are:</p>

<ul>
<li>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?</li>
<li>What are some recommended techniques or libraries to implement this in a C++ program?</li>
</ul>

<h3>List of hooking techniques</h3>

<ul>
<li>proxy with <code>operator-&gt;</code>: <a href="http://www.stroustrup.com/wrapper.pdf" rel="nofollow">stroustrup</a>, <a href="http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/sp_techniques.html#wrapper" rel="nofollow">boost</a></li>
<li><a href="http://coliru.stacked-crooked.com/view?id=2492ef1ee130a80750ba99d0dd9c7290-50d9cfc8a1d350e7409e81e87c2653ba" rel="nofollow">namespace composition</a></li>
<li>preprocessor: <code>#define foo() foo(), log('called foo')</code> <a href="http://coliru.stacked-crooked.com/view?id=79fdf0e8905db6c9a175909024eed72f-50d9cfc8a1d350e7409e81e87c2653ba" rel="nofollow">(more advanced example)</a></li>
</ul>
