views:

116

answers:

1

Ok, which is the best to avoid ambiguity here?

template <class T>
inline void swap(T &a, T &b)
{
    T c; c = a; a = b; b = c;
}
/* blah blah blah (inside of a function:) */
for (itv = vals.begin(); itv != vals.end(); ++itv)
{
    if (at < (*itv)) { swap(at, (*itv)); }
    if (at % (*itv) == 0) atadd = false;
}
/* blah blah blah */

Calling with a swap doesn't work either, as it says cannot resolve whether it is "void swap(T &,T &)", "void std::swap(_Ty &,_Ty &)" or ...

Btw, itv is a vector<int>::iterator.

Thx.

+9  A: 

The problem is namespace std also contains swap() function and looks like you have using namespace std; somewhere earlier, so the compiler can't decide which swap() to use - yours from the global namespace or the one from namespace std.

You need to prepend the call with "::" to explicitly tell the compiler to use your swap() from the global namespace instead of from namespace std. Alternatively you could rename your swap() function or not use using namespace std;.

sharptooth
This brings us to why redefine `swap`, when it is already available in the standard.
David Rodríguez - dribeas