Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that <em>argument-dependent lookup</em> is applied because you are calling a function <em>template</em>. If you had a plain function, there would be no ambiguity:</p> <pre><code>#include &lt;complex&gt; void swap(complex&lt;double&gt; &amp; d, complex&lt;double&gt; &amp; s) { complex&lt;double&gt; temp = d; d = s; s = temp; } int main() { std::complex&lt;double&gt; r(2.4, 3.5), s(3.4, 6.7); swap(r, s); } </code></pre> <p>This is because of C++11 3.4.2:</p> <blockquote> <p>Let <em>X</em> be the lookup set produced by unqualified lookup (3.4.1) and let <em>Y</em> be the lookup set produced by argument dependent lookup (defined as follows). If <em>X</em> contains</p> <p>— a declaration of a class member, or</p> <p>— a block-scope function declaration that is not a using-declaration, or</p> <p>— a declaration that is neither a function or a function template</p> <p>then <em>Y</em> is empty. Otherwise <em>Y</em> is the set of declarations found in the namespaces associated with the argument types as described below. The set of declarations found by the lookup of the name is the union of <em>X</em> and <em>Y</em>.</p> </blockquote> <p>In the above case, <em>X</em> contains a block-scope function declaration.</p> <p>However, if the unqualified lookup finds the function <em>template</em> you provided, then the ADL rules say that the argument-dependent lookup set is also considered. Since <code>std::complex&lt;double&gt;</code> lives in the <code>std</code> namespace, this namespace is also searched for the name <code>swap</code>, which already exists (by 20.2.2).</p> <p>You can prevent argument-dependent lookup by wrapping the function name in parentheses:</p> <pre><code>(swap)(r, s); </code></pre> <p>This way, only the unqualified lookup is considered.</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