why does compiler complain "no matching function for call to 'swap'" once i define move assignment operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class test{
public:
    test(int);
    test(const test&);
    test& operator=(test rhs);
    test(test&&);
    test& operator=(test&& rhs);
private:
    int *p = nullptr;
};

test::test(int i): p(new int(i)){}

test::test(const test& rhs): p(new int(*rhs.p)){}

test& test::operator=(test rhs) {
    using std::swap;
    swap(*this, rhs);
    return *this;
}

test::test(test&& rhs): p(rhs.p){rhs.p = nullptr;}

test& test::operator=(test&& rhs) {
    p = rhs.p;
    rhs.p = nullptr;
    return *this;
}

every thing is right before i define move assignment operator. but once i do, compiler complains "no matching function for call to 'swap'" at line 18
why?
thanks for reading, i really need some help!
Last edited on
your type is no longer move-assignable because overload resolution cannot decide between operator=(test) and operator=(test&&).

Either separate move from copy: operator=(const test&) and operator=(test&&), or combine them as operator=(test).
thank you very much!
Topic archived. No new replies allowed.