How to get around inability to overload on return type in C++
<p>I have function which can create a number of different templated types; an simple example of the problem would be:</p>

<pre><code>EncodedMsg&lt;?&gt;* encode(const Msg&amp; msg)
{
    if(msg.qty &lt; 100) 
        return new EncodedMsg&lt;short&gt;(...);
    else if(msg.qty &lt; 100000)
        return new EncodedMsg&lt;int&gt;(...);
    else 
        return new EncodedMsg&lt;long&gt;(...);
}
</code></pre>

<p>The issue I have is that:</p>

<ol>
<li>The decision as to what to template on is decided inside the function</li>
<li>I can't overload on return type</li>
<li>I don't want to return a base class type of EncodedMsg as I'll then have to decode what type of message it is later on</li>
</ol>

<p>Can anyone think of a way around this? </p>
