title: Virtual template function with templated return value workaround
body: <p>Questions about workarounds for templated functions that must be virtual are quite common here, though I couldn't find anything that helps with my problem, which is a simple variation of this question: <a href="https://stackoverflow.com/questions/2939860/need-a-virtual-template-member-workaround">need a virtual template member workaround</a></p>
  
  <p>The proposed approach is using type erasure leading to a very clean and simple solution. However, what if I need to return a value from the <code>visit()</code> method? The OP already had this aspect in his question, but since he never used the result it was ignored in the solution.</p>
  
  <p>Now imagine this code instead:</p>
  
  <pre><code>template &lt;typename T&gt;
  class BaseVisitor {
    public:
      BaseVisitor();
      T visit(BaseVisited *visited);
      virtual ~BaseVisitor();
  }
  
  
  class BaseVisited {
    BaseVisited();
    template &lt;typename T&gt;
      virtual T accept(BaseVisitor&lt;T&gt; *visitor) { return visitor-&gt;visit(this); };
    virtual ~BaseVisited();
  }
  </code></pre>
  
  <p>We still need <code>accept()</code> to be templated, even after applying the type erasure trick. Any further ideas?</p>
  
  <p>Note: I cannot use a base class for the return value, as has been suggested in some of the answers on SO, because T can stand for any base type as well (int, string etc.).</p>