0

In getting a error using template functions in that generic swap function:

#include <iostream>
#include <complex>

using namespace std;

template <class T>
inline void swap(T& d, T& s)
{
    T temp = d;
    d = s;
    s = temp;
}

int main()
{
    int m = 5, n = 10;
    double x = 5.3, y = 10.6;

    complex<double> r(2.4, 3.5), s(3.4, 6.7);

    cout << "inputs: " << m << "," << n << endl;
    swap(m, n);
    cout << "outputs: " << m << "," << n << endl;

    cout << "double inputs: " << x << "," << y << endl;
    swap(x, y);
    cout << "double outputs: " << x << "," << y << endl;

    cout << "complex inputs: " << r << "," << s << endl;
    swap(r, s);
    cout << "complex outputs: " << r << "," << s << endl;

    return 0;
}

Someone can spot a error here? I getting the error at the three calling functions:

Error C2668 'swap': ambiguous call to overloaded function


I changed to std:: format, that the code:

#include <iostream>
#include <complex>

template <class T>
inline void swap(T& d, T& s)
{
    T temp = d;
    d = s;
    s = temp;
}

int main()
{
    int m = 5, n = 10;
    double x = 5.3, y = 10.6;

    std::complex<double> r(2.4, 3.5), s(3.4, 6.7);

    std::cout << "inputs: " << m << "," << n << std::endl;
    swap(m, n);
    std::cout << "outputs: " << m << "," << n << std::endl;

    std::cout << "double inputs: " << x << "," << y << std::endl;
    swap(x, y);
    std::cout << "double outputs: " << x << "," << y << std::endl;

    std::cout << "complex inputs: " << r << "," << s << std::endl;
    swap(r, s);
    std::cout << "complex outputs: " << r << "," << s << std::endl;

    return 0;
}

And now the full error I got:

SwapFunction.cpp(55,14): error C2668: 'swap': ambiguous call to overloaded function
SwapFunction.cpp(32,13): message : could be 'void swap<std::complex<double>>(T &,T &)'
        with
        [
            T=std::complex<double>
        ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\utility(102,19): message : or       'void std::swap<std::complex<double>,0>(_Ty &,_Ty &) noexcept(<expr>)' [found using argument-dependent lookup]
        with
        [
            _Ty=std::complex<double>
        ]

So I only getting error in the std::complex<double> call function, whats can be done next?

  • 1
    See: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) . – WhozCraig Dec 22 '20 at 13:10

1 Answers1

3

using namespace std is the likely culprit here, since it pulls std::swap into the global namespace.

So you can either remove that and fully qualify the relevant identifiers (recommended) or rename your own swap function.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Thanks Paul, I changed to std:: format(now I see thas a bad practice to use), now I only getting error in the last call. – Bruno Messias Dec 22 '20 at 13:21
  • What error are you getting please (please add the full text to your question). Is it the same as before? – Paul Sanders Dec 22 '20 at 13:23
  • I add now the changes I made, and the error is kind of better, before I getting C2668 at the three functions call, now I only getting at one – Bruno Messias Dec 22 '20 at 13:35
  • 1
    @BrunoMessias The `std::swap` is still found via [ADL](https://en.cppreference.com/w/cpp/language/adl). Either call your function **fully-qualified**, like `::swap(r, s);` or just rename your `swap` to `my_swap`, for example. – heap underrun Dec 22 '20 at 13:47
  • @heapunderrun Thank you! I changed to `::swap`, that's woked – Bruno Messias Dec 22 '20 at 13:55