title: Variadic version of FastDelegate and extra value copy
body: <p>I'm porting <a href="http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible" rel="nofollow">FastDelegate</a> to C++0x using variadic templates.</p>
  
  <pre><code>    #include "FastDelegate.h"
  
      template&lt;class R=fastdelegate::detail::DefaultVoid, class ...P&gt;
      class fast_delegate_base {
      private:
          typedef typename fastdelegate::detail::DefaultVoidToVoid&lt;R&gt;::type desired_ret_t;
          typedef desired_ret_t (*static_func_ptr)(P...);
          typedef R (*unvoid_static_func_ptr)(P...);
          typedef R (fastdelegate::detail::GenericClass::*generic_mem_fn)(P...);
          typedef fastdelegate::detail::ClosurePtr&lt;generic_mem_fn, static_func_ptr, unvoid_static_func_ptr&gt; closure_t;
          closure_t closure_;
      public:
          // Typedefs to aid generic programming
          typedef fast_delegate_base type;
  
          // Construction and comparison functions
          fast_delegate_base() { clear(); }
  
          fast_delegate_base(const fast_delegate_base &amp;x)
          {
              closure_.CopyFrom(this, x.closure_);
          }
  
          void operator = (const fast_delegate_base &amp;x)
          {
              closure_.CopyFrom(this, x.closure_);
          }
          bool operator ==(const fast_delegate_base &amp;x) const
          {
              return closure_.IsEqual(x.closure_);
          }
          bool operator !=(const fast_delegate_base &amp;x) const
          {
              return !closure_.IsEqual(x.closure_);
          }
          bool operator &lt;(const fast_delegate_base &amp;x) const
          {
              return closure_.IsLess(x.closure_);
          }
          bool operator &gt;(const fast_delegate_base &amp;x) const
          {
              return x.closure_.IsLess(closure_);
          }
  
          // Binding to non-const member functions
          template&lt;class X, class Y&gt;
          fast_delegate_base(Y *pthis, desired_ret_t (X::* function_to_bind)(P...) )
          {
              closure_.bindmemfunc(fastdelegate::detail::implicit_cast&lt;X*&gt;(pthis), function_to_bind);
          }
  
          template&lt;class X, class Y&gt;
          inline void bind(Y *pthis, desired_ret_t (X::* function_to_bind)(P...))
          {
              closure_.bindmemfunc(fastdelegate::detail::implicit_cast&lt;X*&gt;(pthis), function_to_bind);
          }
  
          // Binding to const member functions.
          template&lt;class X, class Y&gt;
          fast_delegate_base(const Y *pthis, desired_ret_t (X::* function_to_bind)(P...) const)
          {
              closure_.bindconstmemfunc(fastdelegate::detail::implicit_cast&lt;const X*&gt;(pthis), function_to_bind);
          }
  
          template&lt;class X, class Y&gt;
          inline void bind(const Y *pthis, desired_ret_t (X::* function_to_bind)(P...) const)
          {
              closure_.bindconstmemfunc(fastdelegate::detail::implicit_cast&lt;const X *&gt;(pthis), function_to_bind);
          }
  
          // Static functions. We convert them into a member function call.
          // This constructor also provides implicit conversion
          fast_delegate_base(desired_ret_t (*function_to_bind)(P...) )
          {
              bind(function_to_bind);
          }
  
          // for efficiency, prevent creation of a temporary
          void operator = (desired_ret_t (*function_to_bind)(P...) )
          {
              bind(function_to_bind);
          }
  
          inline void bind(desired_ret_t (*function_to_bind)(P...))
          {
              closure_.bindstaticfunc(this, &amp;fast_delegate_base::invoke_static_func, function_to_bind);
          }
  
          // Invoke the delegate
          template&lt;typename ...A&gt;
          R operator()(A&amp;&amp;... args) const
          {
              return (closure_.GetClosureThis()-&gt;*(closure_.GetClosureMemPtr()))(std::forward&lt;A&gt;(args)...);
          }
          // Implicit conversion to "bool" using the safe_bool idiom
  
      private:
          typedef struct safe_bool_struct
          {
              int a_data_pointer_to_this_is_0_on_buggy_compilers;
              static_func_ptr m_nonzero;
          } useless_typedef;
          typedef static_func_ptr safe_bool_struct::*unspecified_bool_type;
      public:
          operator unspecified_bool_type() const { return empty()? 0: &amp;safe_bool_struct::m_nonzero; }
          // necessary to allow ==0 to work despite the safe_bool idiom
          inline bool operator==(static_func_ptr funcptr) { return closure_.IsEqualToStaticFuncPtr(funcptr); }
          inline bool operator!=(static_func_ptr funcptr) { return !closure_.IsEqualToStaticFuncPtr(funcptr); }
          // Is it bound to anything?
          inline bool operator ! () const { return !closure_; }
          inline bool empty() const { return !closure_; }
          void clear() { closure_.clear();}
          // Conversion to and from the DelegateMemento storage class
          const fastdelegate::DelegateMemento &amp; GetMemento() { return closure_; }
          void SetMemento(const fastdelegate::DelegateMemento &amp;any) { closure_.CopyFrom(this, any); }
  
      private:
          // Invoker for static functions
          R invoke_static_func(P... args) const
          {
              return (*(closure_.GetStaticFunction()))(args...);
          }
      };
  
      // fast_delegate&lt;&gt; is similar to std::function, but it has comparison operators.
      template&lt;typename _Signature&gt;
      class fast_delegate;
  
      template&lt;typename R, typename ...P&gt;
      class fast_delegate&lt;R(P...)&gt; : public fast_delegate_base&lt;R, P...&gt;
      {
      public:
          typedef fast_delegate_base&lt;R, P...&gt; BaseType;
  
          fast_delegate() : BaseType() { }
  
          template&lt;class X, class Y&gt;
          fast_delegate(Y * pthis, R (X::* function_to_bind)(P...))
              : BaseType(pthis, function_to_bind)
          { }
  
          template&lt;class X, class Y&gt;
          fast_delegate(const Y *pthis, R (X::* function_to_bind)(P...) const)
              : BaseType(pthis, function_to_bind)
          { }
  
          fast_delegate(R (*function_to_bind)(P...))
              : BaseType(function_to_bind)
          { }
  
          void operator = (const BaseType &amp;x)
          {
              *static_cast&lt;BaseType*&gt;(this) = x;
          }
      };
  </code></pre>
  
  <p>But, one of the limitations of my implementation is, when using non-member functions, and in case that function accepts parameter(s) by value, an extra value copy for each parameters take place. I assume that this occurs between <code>fast_delegate_base::operator()()</code> and <code>fast_delegate_base::invoke_static_func()</code>. </p>
  
  <p>I tried to make <code>fast_delegate_base::invoke_static_func()</code> to accept Rvalue parameters, but failed.</p>
  
  <p>For example:</p>
  
  <pre><code>class C1
  {
  public:
      C1() { printf("C1()\n"); }
      ~C1() { printf("~C1()\n"); }
      C1(const C1&amp;)
      {
          printf("C1(const C1&amp;)\n");
      }
  
      int test(int t) const
      {
          printf("C1::test(%d)\n", t);
          return 1;
      }
  };
  
  int test(C1 c)
  {
      c.test(1234);
      return 1;
  }
  
  // ...
  
  C1 c1;
  fast_delegate&lt;int(C1)&gt; t1(test);
  t1(c1);
  </code></pre>
  
  <p>Result of this code is:</p>
  
  <pre><code>C1()
  C1(const C1&amp;)
  C1(const C1&amp;)
  C1::test(1234)
  ~C1()
  ~C1()
  ~C1()
  </code></pre>
  
  <p>Do you have any idea to avoid this extra value copy?</p>