title: C++: Operators and Templates
body: <p>Following and example I took from another question, I understand why this does not work:</p>
  
  <pre><code>struct Foo {
      Foo() {}
      Foo(int) {}
      Foo operator+(Foo const &amp; R) { return Foo(); }
  };
  
  
  struct Bar {
      Bar() {}
      Bar(int) {}
  };
  
  Bar operator+(Bar const &amp; L, Bar const &amp; R) {
      return Bar();
  }
  
  
  int main() {
      Foo f;
      f+1;  // Will work - the int converts to Foo
      1+f;  // Won't work - no matching operator
      Bar b;
      b+1;  // Will work - the int converts to Bar
      1+b;  // Will work, the int converts to a Bar for use in operator+
  
  }
  </code></pre>
  
  <p>But then, if I change it to use templates in this way:</p>
  
  <pre><code>template &lt;class T&gt;
  struct Foo {
      Foo() {}
      Foo(T) {}
      Foo operator+(Foo const &amp; R) { return Foo(); }
  };
  
  
  template &lt;class T&gt;
  struct Bar {
      Bar() {}
      Bar(T) {}
  };
  
  template &lt;class T&gt;
  Bar operator+(Bar const &amp; L, Bar const &amp; R) {
      return Bar();
  }
  
  
  int main() {
      Foo&lt;int&gt; f;
      f+1;  // Will work - the int converts to Foo
      1+f;  // Won't work - no matching operator
      Bar&lt;int&gt; b;
      b+1;  // DOES NOT WORK
      1+b;  // DOES NOT WORK
  </code></pre>
  
  <p>}</p>
  
  <p>It does not work. Can anyone put some light on this? Templates are driving me crazy.</p>
  
  <p>Thanks.</p>