title: C++ - Define Copy Constructor in Template
body: <p>I'm a beginner in template coding, and having trouble defining constructor in template, and just looked for answers about that but cannot find related questions.</p>
  
  <p>Basically the class/struct <code>xpair</code> is similar to <code>pair</code> which has <code>first</code> and <code>second</code>.</p>
  
  <pre><code>template &lt;typename First, typename Second&gt;
  struct xpair {
     First first{};
     Second second{};
  
     xpair(){}
     xpair (const First&amp; first, const Second&amp; second):
                first(first), second(second) {}
  
     xpair&amp; operator() (const First&amp; first_, const Second&amp; second_) {
        return new xpair (first_, second_);
     }
  
     xpair&amp; operator= (const xpair&amp; that) {
        return new xpair (that.first, that.second);
     }
  };
  </code></pre>
  
  <p>When I'm trying to write something like </p>
  
  <pre><code>xpair&lt;string, string&gt; a, b;
  a = b;
  </code></pre>
  
  <p>It gives error as </p>
  
  <pre><code>non-const lvalue reference to type
  'xpair&lt;std::__1::basic_string&lt;char&gt;, std::__1::basic_string&lt;char&gt; &gt;'
  cannot bind to a temporary of type 'xpair&lt;std::__1::basic_string&lt;char&gt;,
  std::__1::basic_string&lt;char&gt; &gt; *'
  </code></pre>
  
  <p>I tried rewrite </p>
  
  <pre><code>return new xpair (that.first, that.second);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>return new pair (const_cast&lt;First&amp;&gt;(that.first), const_cast&lt;First&amp;&gt;(that.second));
  </code></pre>
  
  <p>But it doesn't work. Where's the problem?</p>