title: overload resolution of templated special member (operator=)
body: <p><strong>Context</strong>: In an attempt to implement the <a href="https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom">copy and swap</a> idiom on a templated homemade shared_ptr. I'm having trouble to get this function called:</p>
  
  <pre><code>    template&lt;typename U&gt;
      shared_ptr&lt;T&gt;&amp; operator=(shared_ptr&lt;U&gt; rhs)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value);
          swap(*this, rhs);
          return *this;
      }
  </code></pre>
  
  <p><strong>Code stripped from the unecessary:</strong></p>
  
  <p>The following code does not compile. The "x=y" line is an attempt to use:</p>
  
  <pre><code>    foo&lt;T&gt;&amp; operator=(const foo&lt;U&gt;&amp; ptr) = delete;
  </code></pre>
  
  <p>If I remove/comment the deleted functions line. Meaning if I do not define them, the compiler will do it for me and the code will compile. However,the special member generated by the compiler will be preferred to my operator which will not be called.</p>
  
  <pre><code>template&lt;typename T&gt; 
  class foo
  {
  public:
  
      // copy&amp;swap
      template&lt;typename U&gt;
      foo&lt;T&gt;&amp; operator=(foo&lt;U&gt; rhs)
      {
          // whatever is needed swap and all
          return *this;
      }
  
      template&lt;typename U&gt;
      foo&lt;T&gt;&amp; operator=(const foo&lt;U&gt;&amp; ptr) = delete;
      template&lt;typename U&gt;
      foo&lt;T&gt;&amp; operator=(foo&lt;U&gt;&amp;&amp; ptr) = delete;
  
      foo&lt;T&gt;&amp; operator=(foo&lt;T&gt;&amp;&amp; ptr) = delete;
  };
  
  int main()
  {
      foo&lt;int&gt; x,y;
      x = y;
      return 0;
  }
  </code></pre>
  
  <p><strong>Questions</strong>:</p>
  
  <ul>
  <li>The overload resolution is not clear to me here.  Why is one signature preferred to the other? </li>
  <li>If the function is deleted, I understand it is not a "substitution failure", but still it is a failure at signature level, not inside the body, why does the compiler stop looking for a match?</li>
  <li>Any solution to get the function to be called ? </li>
  </ul>
  
  <hr>
  
  <hr>
  
  <p><strong>Edit/Answer</strong></p>
  
  <p>This is the correct version so far:</p>
  
  <ul>
  <li>Non-template constructors (move/copy) are marked explicit so that assigning foo into foo calls the template version.</li>
  <li>Appropriate static assert are added so conversion U/T makes sense</li>
  <li>Arguments are perfect forwarded to the assign/move internal functions</li>
  </ul>
  
  <p>code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  template&lt;typename T&gt;
  class foo
  {
  private:
      template &lt;typename U&gt; friend class foo;
  
      template&lt;typename U&gt;
      void assign(const foo&lt;U&gt;&amp; other)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value || std::is_convertible&lt;U, T&gt;::value);
          std::cout &lt;&lt; "templated assign function" &lt;&lt; std::endl;
          this-&gt;data = other.data;
      }
  
      template&lt;typename U&gt;
      void move(foo&lt;U&gt;&amp;&amp; other)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value || std::is_convertible&lt;U, T&gt;::value);
          std::cout &lt;&lt; "templated move function" &lt;&lt; std::endl;
          this-&gt;swap(*this, other);
      }
  public:
  
      template&lt;class X, class Y&gt; void swap(foo&lt;X&gt;&amp; left, foo&lt;Y&gt;&amp; right) noexcept
      {
          std::cout &lt;&lt; "templated swap function" &lt;&lt; std::endl;
          std::swap(left.data, right.data);
      }
  
      void swap(foo&lt;T&gt;&amp; left, foo&lt;T&gt;&amp; right) noexcept
      {
          std::cout &lt;&lt; "swap function" &lt;&lt; std::endl;
          std::swap(left.data, right.data);
      }
  
      foo() {}
  
      explicit foo(foo&lt;T&gt;&amp;&amp; other)
      {
          std::cout &lt;&lt; "move constructor foo(foo&amp;&amp; other)" &lt;&lt; std::endl;
          move(std::forward&lt;decltype(other)&gt;(other));
      }
  
  
      explicit foo(const foo&lt;T&gt;&amp; other)
      {
          std::cout &lt;&lt; "copy constructor foo(const foo&amp; other)" &lt;&lt; std::endl;
          assign(std::forward&lt;decltype(other)&gt;(other));
      }
  
      template&lt;typename U&gt;
      foo(foo&lt;U&gt;&amp;&amp; other)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value || std::is_convertible&lt;U, T&gt;::value);
          std::cout &lt;&lt; "templated move constructor template&lt;typename U&gt; foo(foo&lt;U&gt;&amp;&amp; other)" &lt;&lt; std::endl;
          move(std::forward&lt;decltype(other)&gt;(other));
      }
  
      template&lt;typename U&gt;
      foo(const foo&lt;U&gt;&amp; other)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value || std::is_convertible&lt;U,T&gt;::value);
          std::cout &lt;&lt; "templated copy constructor template&lt;typename U&gt; foo(const foo&lt;U&gt;&amp; other)" &lt;&lt; std::endl;
          assign(std::forward&lt;decltype(other)&gt;(other));
      }
  
      // copy&amp;swap
      template&lt;typename U&gt;
      foo&lt;T&gt;&amp; operator=(foo&lt;U&gt; rhs)
      {
          static_assert(std::is_base_of&lt;T, U&gt;::value || std::is_convertible&lt;U, T&gt;::value);
  
          std::cout &lt;&lt; "templated assignement template&lt;typename U&gt; foo&lt;T&gt;&amp; operator=(foo&lt;U&gt; rhs)" &lt;&lt; std::endl;
  
          auto tmp = rhs.data;
          rhs.data = reinterpret_cast&lt;U*&gt;(data);
          data = reinterpret_cast&lt;T*&gt;(tmp);
  
          return *this;
      }
  
      foo&lt;T&gt;&amp; operator=(foo&lt;T&gt; rhs)
      {
          std::cout &lt;&lt; "assignement foo&lt;T&gt;&amp; operator=(foo&lt;T&gt; rhs)" &lt;&lt; std::endl;
  
          std::swap(rhs.data,data);
          return *this;
      }
  
  private:
      T * data;
  };
  
  int main()
  {
      foo&lt;int&gt; x,y;
      const foo&lt;int&gt;&amp; cy = y;
  
      foo&lt;short&gt; z, w;
      x = y;
      x = cy;
      x = std::move(y);
      x = z;
      x = std::move(w);
      return 0;
  }
  </code></pre>