title: Command pattern without virtual functions (C++)
body: <p>For performance reasons, I am using the the <a href="http://ubiety.uwaterloo.ca/~tveldhui/papers/techniques/techniques01.html" rel="nofollow noreferrer">Curiously Reoccuring Template Pattern</a> to avoid virtual functions.  I have lots of small commands which execute millions of times.  I am trying to fit this into the Command Pattern.  I want to add tons of commands to a queue, and then iterate through them executing each one by one.  Each Command uses a CRTP to avoid virtual functions.  The problem I am running into is that the Command pattern is typically implemented using a vector of pointers.  But when the Command class is templated, it becomes hard to pass around generic Command pointers. I'm not a C++ expert, so perhaps there is an obvious way to store a vector of templated command objects?  I have been trying to use something like:</p>
  
  <pre><code>boost:ptr_vector commands;
  AddCommand(Command* command) {
    commands.push_back(command);
  }
  </code></pre>
  
  <p>The problem is <code>Command</code> is not a type, so <code>Command* command</code> gives a compile error.  I need to use <code>Command&lt;CommandType&gt;</code>, but that won't work because I need the queue to hold different types of commands. </p>
  
  <p>Any ideas for solutions?  Or are virtual functions my only option?</p>
  
  <p>ADDED:  The command objects are part of a monte carlo simulation algorithm.  So you might have, Command be a random number from a normal distribution, where the parameters of the normal distribution are part of the class.  So the command pattern fits very nicely.  I have lots of calls, in a particular order, to functions that need to maintain state.</p>