Here's my case:
I am trying to use a library that has a type Foo::a
, and specifies a Foo::swap
as well. Another library that I am consuming has a std::vector<Foo::a>
instantiation. I am trying to compile this on Windows using Visual Studio 11.0 and notice that the std::vector::swap
maps down to _Swap_adl
which does an unqualified swap call.
This is what gets me into issues with ADL and ambiguous function resolutions. Is there some magic that will allow me to use Foo::swap
(heck even std::swap
:)), without making some "major" change to the libraries that I am consuming (stuff that is short of removing/renaming swap from Foo, etc)?
Edit: Adding a minimal example that captures what is going on and the error.
#include <iostream>
#include <vector>
namespace Foo
{
class MyType
{
public:
float dummy;
};
template <class T>
void swap(T& a, T& b)
{
T c(a);
a = b;
b = c;
}
}
using namespace Foo;
class MyClass
{
public:
std::vector<MyType> myStructArr;
};
std::vector<MyType> getMyTypeArray()
{
MyType myType;
std::vector<MyType> myTypeArray;
myTypeArray.push_back(myType);
return myTypeArray;
}
namespace std
{
template <>
void swap<MyType*>(MyType*& a, MyType*& b)
{
MyType* c(a);
a = b;
b = c;
}
template <>
void swap<MyType>(MyType& a, MyType& b)
{
MyType c(a);
a = b;
b = c;
}
}
int main(int argc, char* argv[])
{
MyClass m;
MyType myTypeLocal;
std::vector<MyType> myTypeArrayLocal;
myTypeArrayLocal.push_back(myTypeLocal);
//m.myStructArr = myTypeArrayLocal;
m.myStructArr = getMyTypeArray();
return 0;
}
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 @ http://pastebin.com/Ztea46aC 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?
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.
namespace std
{
template <>
void swap<MyType*>(MyType*& a, MyType*& b)
{
MyType* c(a);
a = b;
b = c;
}
template <>
void swap<MyType>(MyType& a, MyType& b)
{
MyType c(a);
a = b;
b = c;
}
}
http://pastebin.com/sMGDZQBZ is the error log from this attempt.