title: Return-type polymorphism for pass-by-value
body: <p>I'm not sure if the question title is accurate... Let me start by explaining my original simple scenario, and then move on to explain what would I like to do, but can't.</p>
  
  <p>Originally, I had something like:</p>
  
  <pre><code>class Operand;
  
  Operand genOperandA() { ...; return Operand(); }
  Operand genOperandB() { ...; return Operand(); }
  ... // more operand-generation functions
  
  typedef Operand (*OpGen)();
  
  // Table of function pointers
  static const OpGen generators[] =
  {
      genOperandA,
      genOperandB,
      ...
  };
  
  // Function to do some operation on the operand
  void operate(Operand&amp; op);
  
  ...
  
  // Example call
  operate(generators[1]());
  </code></pre>
  
  <p>So far so good (I think).  However, there are now several derived operand types, e.g. <code>class RegisterOperand : public Operand</code>.  I have new, dedicated <code>genOperand</code> functions that ideally would return instances of the derived types.  But I can't do this:</p>
  
  <pre><code>Operand genOperandC() { ...; return RegisterOperand(); }
  </code></pre>
  
  <p>and I can't do this:</p>
  
  <pre><code>RegisterOperand genOperandC() { ...; return RegisterOperand(); }
  
  static const OpGen generators[] = 
  {
      ...
      genOperandC,
  };
  </code></pre>
  
  <p>However, I know this would work if I were to return reference or pointer types, so the only option I currently have is something like:</p>
  
  <pre><code>Operand *genOperandC() { ...; return new RegisterOperand(); }
  </code></pre>
  
  <p>which now requires explicit cleanup which wasn't necessary originally.</p>
  
  <p>Any alternatives I haven't considered?</p>