Implicit conversion to a C++ template object
<p>I have a class A:</p>

<pre><code>template &lt;typename T, int I&gt; struct A {};
</code></pre>

<p>and a class B. I would like object's of type B to implicitly convert to A when given as function arguments. B looks like this:</p>

<pre><code>template &lt;typename T&gt;
struct B {
  operator A&lt;T,0&gt; &amp;() const { return *new A&lt;T,0&gt;(); }
};
</code></pre>

<p>However, my test (below) fails with GCC 4.5, giving the error: <em>no matching function for call to 'test(B&amp;)'</em> Where am I going wrong here? Do other compilers also reject this?</p>

<pre><code>template &lt;typename T, int I&gt;
void test(A&lt;T,I&gt; &amp;a) { delete &amp;a; }

int main(int argc, char *argv[])
{
  B&lt;int&gt; b;
  test(b);
  return 0;
}
</code></pre>

<p>p.s. I've now put my own solution in an answer below.</p>
