title: C++ template programming - Deferred function calls
body: <p>I am looking for an elegant solution to the following problem. I have a task struct that I use for deferred function calls.</p>
  
  <pre><code>template &lt;typename T&gt; struct Task1
  {
      T Arg1;
      Delegate&lt;T&gt; TaskDelegate;
  };
  </code></pre>
  
  <p>The problem I'm having is this:</p>
  
  <pre><code>Task1&lt;const Foo&amp;&gt; MyTask;
  </code></pre>
  
  <p>This will result in the parameter being held as a const reference. Does anyone know a nice solution to get round this? I could enforce rules such as the delegate signature always taking const&amp; params but this seems restrictive. I could always have two task structs (one for ref and one for value) but this seems nasty.</p>
  
  <p>The other solution would be to create the following:</p>
  
  <pre><code>template &lt;typename T1, typename T2&gt; struct Task1
  {
      T2 Arg1;
      Delegate&lt;T1&gt; TaskDelegate;
  };
  </code></pre>
  
  <p>Is there anyway to default T2 to be the same type as T1? That way whenever I have a method value signature I don't need to have the additional template params.</p>
  
  <p>EDIT:
  The template is used for a multithreaded task scheduler. Here is an example:</p>
  
  <pre><code>void MyClass::Populate(const std::string&amp; instrText);
  
  CTaskScheduler::Schedule(Task1&lt;const std::string&amp;&gt;(this, &amp;MyClass::Popluate, "MyString"));
  </code></pre>