#include <iostream>
#include <string>
using namespace std;
class A
{
std::string m_Data;
public:
explicit A(std::string data = "HEHE")
: m_Data(std::move(data))
{}
A(A&& other)
{
if (other.m_Data.empty())
return;
m_Data = std::move(other.m_Data);
}
A& operator=(A&& other)
{
if (m_Data.empty() || other.m_Data.empty())
return *this;
m_Data = std::move(other.m_Data);
return *this;
}
const std::string& Data() const { return m_Data; }
};
template<typename T>
void MySwap(T& x, T& y)
{
T z = std::move(x);
x = std::move(y);
y = std::move(z);
}
int main() {
A a("A"), b("B");
cout << "a=" << a.Data() << ", b=" << b.Data() << endl;
MySwap(a, b);
cout << "after MySwap: a=" << a.Data() << ", b=" << b.Data() << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIEEKewoJc3RkOjpzdHJpbmcgbV9EYXRhOwoJCnB1YmxpYzoKCWV4cGxpY2l0IEEoc3RkOjpzdHJpbmcgZGF0YSA9ICJIRUhFIikKCSAgOiBtX0RhdGEoc3RkOjptb3ZlKGRhdGEpKQoJe30KCQoJQShBJiYgb3RoZXIpCgl7CgkJaWYgKG90aGVyLm1fRGF0YS5lbXB0eSgpKQoJCSAgcmV0dXJuOwoJCSAgCgkJbV9EYXRhID0gc3RkOjptb3ZlKG90aGVyLm1fRGF0YSk7Cgl9CgkKCUEmIG9wZXJhdG9yPShBJiYgb3RoZXIpCgl7CgkJaWYgKG1fRGF0YS5lbXB0eSgpIHx8IG90aGVyLm1fRGF0YS5lbXB0eSgpKQoJCSAgcmV0dXJuICp0aGlzOwoJCSAgCgkJbV9EYXRhID0gc3RkOjptb3ZlKG90aGVyLm1fRGF0YSk7CgkJcmV0dXJuICp0aGlzOwoJfQoJCgljb25zdCBzdGQ6OnN0cmluZyYgRGF0YSgpIGNvbnN0IHsgcmV0dXJuIG1fRGF0YTsgfQp9OwoKdGVtcGxhdGU8dHlwZW5hbWUgVD4Kdm9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