title: What's the equivalent of Python function decorators in C++?
body: <p>I'm porting a Python metrics library to C++.  One API the Python library provides is a function decorator to easily record timing data on functions.  By modifying the function definition to be</p>
  
  <pre><code>@timed('timing.foo')
  def foo():
      ...
  </code></pre>
  
  <p><code>foo_result = foo()</code> is essentially turned into</p>
  
  <pre><code>start = time.time()
  foo_result = foo()
  post_metric_to_endpoint('timing.foo', time.time() - start)
  </code></pre>
  
  <hr>
  
  <p>In <a href="https://stackoverflow.com/q/7743771/120999">Function hooking in C++</a>, we find <a href="https://stackoverflow.com/a/7744342/120999">this answer</a> that wraps instances and places the burden of calling the timing function on the caller, which means we won't get performance data across the codebase (it's annoying to update in the first place, and easily forgotten later).  Similarly, <a href="https://stackoverflow.com/a/31392034/120999">this answer</a> to <a href="https://stackoverflow.com/q/31391914/120999">Timing in an elegant way in c++</a> requires altering the call sites.  <a href="https://stackoverflow.com/a/31392259/120999">This other answer</a> to that question provides a way of wrapping arbitrary blocks of code, which theoretically means we could indent the entire body of a function we want to time and nest it inside a scope.  This is the closest I've found to what I want, but is pretty ugly, fairly intrusive, and I'm not sure of the performance impact.</p>
  
  <p>Since this is meant to be used as a library, we can modify the source of the functions we want to time; in fact, this is preferable, as I'd like <em>every</em> call of that function to be timed.  Most discussions seem to focus on ad-hoc profiling in development, whereas I'm trying to build a system that would be always active in a production environment.</p>