Template friend function and return type deduction <p>Note: This question is really close to <a href="http://stackoverflow.com/questions/18931993/return-type-deduction-for-in-class-friend-functions">Return type deduction for in-class friend functions</a>, but I did not find the answer to my problem there.</p> <p>Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z</p> <p>This code compiles:</p> <pre><code>#include &lt;iostream&gt; template&lt;class T&gt; class MyClass { public: MyClass(T const&amp; a) : impl(a) {} template&lt;class T0, class T1&gt; friend auto // requires operator+(T0,T1) exists operator+(MyClass&lt;T0&gt; const&amp; a, MyClass&lt;T1&gt; const&amp; b) { return MyClass&lt;decltype(a.impl+b.impl)&gt;{a.impl + b.impl}; } T getImpl() const { return impl; } private: T impl; }; int main() { MyClass&lt;int&gt; x(2); MyClass&lt;long&gt; y(2); auto z = x+y; std::cout &lt;&lt; z.getImpl() &lt;&lt; "\n"; } </code></pre> <p>Now if I define operator+ outside of the class, it does not compile anymore:</p> <pre><code>template&lt;class T&gt; class MyClass { public: MyClass(T const&amp; a) : impl(a) {} template&lt;class T0, class T1&gt; friend auto operator+(MyClass&lt;T0&gt; const&amp; a, MyClass&lt;T1&gt; const&amp; b); T getImpl() const { return impl; } private: T impl; }; template&lt;class T0, class T1&gt; auto operator+(MyClass&lt;T0&gt; const&amp; a, MyClass&lt;T1&gt; const&amp; b) { return MyClass&lt;decltype(a.impl+b.impl)&gt;{a.impl + b.impl}; } </code></pre> <p>Clang 3.4 says:</p> <pre><code>error: use of overloaded operator '+' is ambiguous (with operand types MyClass&lt;int&gt; and MyClass&lt;long&gt;) </code></pre> <p>And then points at what it believes to be two different functions: the declaration in the class and the definition outside the class.</p> <p>My question is: is it a clang bug, or just that template parameters are deduced for a friend function thus leading the two functions not being equivalent is some cases ? And what alternative would you suggest: make operator+ a member function, or define friend operator+ inside the class (which would in my opinion clutter the class interface) ?</p> <p>Just for your information, I have a real use case of such code, where I try to wrap a third -party matrix class and I need return type deduction because of the use of expression template for lazy evaluation.</p> <p><em>Edit</em>: The following does work (but still clutters the interface...)</p> <pre><code>template&lt;typename T&gt; class MyClass { T impl; public: explicit MyClass(T a) : impl(std::move(a)) { } T const&amp; getImpl() const { return impl; } template&lt;typename T0, typename T1&gt; friend auto operator +(MyClass&lt;T0&gt; const&amp; a, MyClass&lt;T1&gt; const&amp; b) -&gt; MyClass&lt;decltype(a.impl + b.impl)&gt;; }; template&lt;typename T0, typename T1&gt; auto operator +(MyClass&lt;T0&gt; const&amp; a, MyClass&lt;T1&gt; const&amp; b) -&gt; MyClass&lt;decltype(a.impl + b.impl)&gt; { return MyClass&lt;decltype(a.impl + b.impl)&gt;(a.impl + b.impl); } </code></pre>