Template function having its caller's context?
<p>Consider the following code snippet.</p>

<pre><code>template &lt;T&gt;
MyPtr&lt;T&gt; CreateObject()
{
    // Do something here first...

    // return our new object
    return MyPtr&lt;T&gt;(new T());
}

class Foo
{
private:
    Foo() { }

public:
    static MyPtr&lt;Foo&gt; GetNewInstance() 
    {
        // ERROR: Foo is private...
        return CreateObject&lt;Foo&gt;();
    }
};

class Bar
{
public:
    Bar() { }
};

int main()
{
    MyPtr&lt;Bar&gt; bar = CreateObject&lt;Bar&gt;();

    return 0;
}
</code></pre>

<p>Without resorting to macro for CreateObject (I like the syntax of <code>MyPtr&lt;type&gt; obj = CreateObject&lt;type&gt;(params)</code>), is there a way to make the function CreateObject share the same context as the caller function, thus able to access private Foo c'tor? 'friend' is not what I'm looking for as it would mean anyone calling CreateObject would have access to private Foo c'tor, which is not what I want. Overloading the new operator wouldn't work either as it is imperative that a MyPtr is returned instead of just T* (by assigning T* to MyPtr assigns a type to the object that is required somewhere else).</p>

<p>I guess what I'm looking for is something in between a macro and a template function (syntax of a template function but gets expanded fully like a macro). It would be quite useful to have this feature in this particular case.</p>
