title: Using std::function/mem_fn in C++11 with member functions
body: <p>Long story short, is there a simple/defined way to treat function objects/lambdas and member functions in a streamlined way?</p>
  
  <p>If I understand right, if I use std::mem_fn, I need to pass an object of the correct type to the function call, i.e.</p>
  
  <pre><code>Object o;
  ftncall std::mem_fun(&amp;Object::function);
  ftncall(o);
  </code></pre>
  
  <p>Ideally, there would be some way to 'attach' <code>o</code> to that function object, perhaps as a <code>std::weak_ptr</code>, so we know if o got deleted.  For example, if there were a way to do something vaguely like this:</p>
  
  <pre><code>Object o;
  ftncall std::mem_fn(&amp;Object::function, o); // Or maybe std::mem_fn&lt;Object&gt;
  ftncall();
  </code></pre>
  
  <p>Now, obviously, this doesn't exist (to my knowledge).  Is there a way to wrap std::mem_fn in such a way that I don't loose the generality (and niceness) of std::mem_fn, but can 'attach' <code>o</code>, AND still play nicely with the other function type, like std::function?  Ideally, I'd still be able to use <code>operator()</code> in the way that I do with std::function.</p>
  
  <p>Currently the best solution I can think of is a class that looks like this:</p>
  
  <pre><code>template&lt;class T&gt;
  class MemFunWrapper {
  public:
      MemFunWrapper(T* t, std::mem_fun funct) : m_t(t), m_function(funct) {}
  
      std::mem_fun operator*() { return m_function; }
      T* get() { return m_t; }
  private:
      T* m_t;
      std::mem_fun m_function;
  }
  </code></pre>
  
  <p>Then you could use it thusly:</p>
  
  <pre><code>(*MemFunWrapper)(MemFunWrapper-&gt;get(), args...);
  </code></pre>
  
  <p>But that seems pretty unwieldy to me.  Also, I'd have to create an equivalent class for std::function for that to be usable in the analogous way, and that seems silly since I can already just use std::function.  Ideally, I'd also be able to use the end product without knowing if I'm calling a member function or a regular function.  I know I'm asking a lot - any direction would be helpful.  Many thanks!</p>