Calling an overloaded function is ambiguous, although a different namespace

I understand why the following will be a problem if no namespace has been used. The challenge would be ambiguous. I thought, "using stD :: swap;" will determine which method to use.

Why does this work for "int" but not for "class"?

    #include <memory>

    namespace TEST {

    class Dummy{};

    void swap(Dummy a){};
    void sw(int x){};

    }

    namespace stD {

    void swap(TEST::Dummy a){};
    void sw(int x){};

    class aClass{
    public:
        void F()
        {
            using stD::swap;
            TEST::Dummy x;
            swap(x);
        }

        void I()
        {
            using stD::sw;
            int b = 0;
            sw(b);
        }
    };

    }

This error message is:

    ../src/Test.h: In member function ‘void stD::aClass::F()’:
    ../src/Test.h:26:9: error: call of overloaded ‘swap(TEST::Dummy&)’ is ambiguous
       swap(x);
             ^
    ../src/Test.h:26:9: note: candidates are:
    ../src/Test.h:17:6: note: void stD::swap(TEST::Dummy)
     void swap(TEST::Dummy a){};
          ^
    ../src/Test.h:10:6: note: void TEST::swap(TEST::Dummy)
     void swap(Dummy a){};
          ^

I really thank you for your reply.

+4
source share
1 answer

This line uses an argument-dependent search.

TEST::Dummy x;
swap(x);

Thus, he will find both void stD::swap(TEST::Dummy)and void TEST::swap(TEST::Dummy)because he xcarries a namespace TEST::.

int b = 0; b , stD::sw - using.

+7

Source: https://habr.com/ru/post/1687234/


All Articles