Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Interestingly, when I compile your code (Visual C++ 10 Express) I get errors, but also when I try this simpler case:</p> <pre><code>int main(int argc, char* argv[]) { C&lt;int&gt; x; // OK C&lt;const int&gt; x1; // error C2535: 'void C&lt;T&gt;::f(T &amp;)' : member function // already defined or declared return 0; } </code></pre> <p>Seems like the ref-to-ref collapsing defined in the DR you mentioned means that <code>const</code> ref becomes a simple non-const ref within the template. My problem with this is that I don't understand why the second <code>f</code> is not just ignored.</p> <p>If I change C so that the second f is <code>const</code>-qualified, this now compiles:</p> <pre><code>template &lt;typename T&gt; struct C { void f(T&amp;) { } void f(const T&amp; t) const {} }; </code></pre> <p>The implication seems to be that when <code>C</code> is instantiated with <code>const</code> anything (ref or not), the two <code>C::f</code> overloads are simply identical, and result in compile-time duplicate detection.</p> <p>Perhaps somebody smarter than me can decipher the chain more definitively here.</p> <p>EDIT: On reflection, it's not surprising here that <code>T = const int&amp;</code> results in the <code>f</code> overloads being identically instantiated as </p> <pre><code>void f(const int&amp;) {} </code></pre> <p>That's what the compiler is telling me:</p> <pre><code>#include "stdafx.h" template &lt;typename T&gt; struct C { void f(T&amp;) { } void f(const T&amp;) { } }; int main() { C&lt;const int&amp;&gt; z; // compile error: f cannot be overloaded return 0; } </code></pre> <p>gives this error:</p> <pre><code>1&gt;test.cpp(6): error C2535: 'void C&lt;T&gt;::f(T)' : member function already defined or declared 1&gt; with 1&gt; [ 1&gt; T=const int &amp; 1&gt; ] 1&gt; test.cpp(5) : see declaration of 'C&lt;T&gt;::f' 1&gt; with 1&gt; [ 1&gt; T=const int &amp; 1&gt; ] 1&gt; test.cpp(10) : see reference to class template instantiation 'C&lt;T&gt;' being compiled 1&gt; with 1&gt; [ 1&gt; T=const int &amp; 1&gt; ] </code></pre> <p>I'm not even convinced this has anything to do with the DR.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload