Note that there are some explanatory texts on larger screens.

plurals
  1. PONon-obtrusive way of getting around an argument dependent lookup ambiguity
    text
    copied!<p>Here's my case:</p> <p>I am trying to use a library that has a type <code>Foo::a</code>, and specifies a <code>Foo::swap</code> as well. Another library that I am consuming has a <code>std::vector&lt;Foo::a&gt;</code> instantiation. I am trying to compile this on Windows using Visual Studio 11.0 and notice that the <code>std::vector::swap</code> maps down to <code>_Swap_adl</code> which does an unqualified swap call.</p> <p>This is what gets me into issues with ADL and ambiguous function resolutions. Is there some magic that will allow me to use <code>Foo::swap</code> (heck even <code>std::swap</code> :)), without making some "major" change to the libraries that I am consuming (stuff that is short of removing/renaming swap from Foo, etc)?</p> <p>Edit: Adding a minimal example that captures what is going on and the error. </p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; namespace Foo { class MyType { public: float dummy; }; template &lt;class T&gt; void swap(T&amp; a, T&amp; b) { T c(a); a = b; b = c; } } using namespace Foo; class MyClass { public: std::vector&lt;MyType&gt; myStructArr; }; std::vector&lt;MyType&gt; getMyTypeArray() { MyType myType; std::vector&lt;MyType&gt; myTypeArray; myTypeArray.push_back(myType); return myTypeArray; } namespace std { template &lt;&gt; void swap&lt;MyType*&gt;(MyType*&amp; a, MyType*&amp; b) { MyType* c(a); a = b; b = c; } template &lt;&gt; void swap&lt;MyType&gt;(MyType&amp; a, MyType&amp; b) { MyType c(a); a = b; b = c; } } int main(int argc, char* argv[]) { MyClass m; MyType myTypeLocal; std::vector&lt;MyType&gt; myTypeArrayLocal; myTypeArrayLocal.push_back(myTypeLocal); //m.myStructArr = myTypeArrayLocal; m.myStructArr = getMyTypeArray(); return 0; } </code></pre> <p>I won't comment on the efficiency of the code, as its something that I just have to work with, but the error log at @ <strong><a href="http://pastebin.com/Ztea46aC" rel="nofollow">http://pastebin.com/Ztea46aC</a></strong> gives a fair idea of what's going on internally. Is this a compiler specific issue, or is there a deeper learning to be gained from this piece of code?</p> <p>Edit 2: I've tried specializing for the particular type in question, but that doesn't resolve the ambiguity. Any pointers on why this is so would be helpful as well.</p> <pre><code>namespace std { template &lt;&gt; void swap&lt;MyType*&gt;(MyType*&amp; a, MyType*&amp; b) { MyType* c(a); a = b; b = c; } template &lt;&gt; void swap&lt;MyType&gt;(MyType&amp; a, MyType&amp; b) { MyType c(a); a = b; b = c; } } </code></pre> <p><strong><a href="http://pastebin.com/sMGDZQBZ" rel="nofollow">http://pastebin.com/sMGDZQBZ</a></strong> is the error log from this attempt.</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