title: c++ implicit conversion on user-defined operator for template classes
body: <p>I have a struct template <code>A&lt;x&gt;</code> and a <code>+</code> operator with <code>int</code>.</p>
  
  <pre><code>#include &lt;iostream&gt;
  template&lt;int x&gt;
  struct A{
      int a;  
  };
  template&lt;int x&gt;
  int operator+(A&lt;x&gt; a, int b){
      return a.a+b;
  }
  </code></pre>
  
  <p>I created a struct template <code>B&lt;x&gt;</code>, which is convertible to <code>A&lt;x&gt;</code>.</p>
  
  <pre><code>template&lt;int x&gt;
  struct B{
      int b=3;
      operator A&lt;x&gt;(){
          return {b+10};
      }
  };
  </code></pre>
  
  <p>Now I want <code>B&lt;x&gt;</code> to be converted to <code>A&lt;x&gt;</code> when calling <code>B&lt;x&gt; + int</code>.</p>
  
  <pre><code>int main(){
      std::cout&lt;&lt;(A&lt;12&gt;{9}+10)&lt;&lt;std::endl;//OK
      std::cout&lt;&lt;(B&lt;12&gt;{9}+10)&lt;&lt;std::endl;//Error
      return 0;
  }
  </code></pre>
  
  <p>I read <a href="https://stackoverflow.com/questions/8890051/implicit-conversion-when-overloading-operators-for-template-classes">Implicit conversion when overloading operators for template classes</a> and wrote</p>
  
  <pre><code>template&lt;int x&gt;
  struct B{
      int b=3;
      operator A&lt;x&gt;(){
          return {b+10};
      }
      friend int operator+(A&lt;x&gt; a, int b);
  };
  </code></pre>
  
  <p>, but it didn't work because the declared <code>friend int operator+(A&lt;x&gt; a, int b)</code> does not match <code>template&lt;int x&gt; int operator+(A&lt;x&gt; a, int b)</code>.</p>
  
  <p>I read <a href="https://stackoverflow.com/questions/33861151/c-how-to-declare-a-function-template-friend-for-a-class-template">C++ - How to declare a function template friend for a class template</a> and made friend declaration template, but it didn't work because the template parameter couldn't be deduced.</p>
  
  <p>Of course I could write operator+ for both A and B, but I have dozens of operators and I don't want to do it.</p>
  
  <p>What is the correct way to do this?</p>