#include <iostream>
using namespace std;
struct SomeObject
{
SomeObject() : value(0), handle(0) {}
SomeObject(const SomeObject& o) : value(o.value), handle(o.handle) {
std::cout << "Copy construct from " << this << " to " << &o;
}
SomeObject(SomeObject&& o) : value(o.value), handle(o.handle) {
std::cout << "Move construct from " << this << " to " << &o;
}
void operator=(SomeObject const &o)
{
std::cout << "Copy from " << this << " to " << &o;
}
void operator=(SomeObject &&o)
{
std::cout << "Move from " << this << " to " << &o;
}
/* example data */
int value;
int handle;
};
SomeObject copy_maybe(bool make_new, const SomeObject& def)
{
if (make_new)
return SomeObject();
else
return def;
}
int main() {
SomeObject obj;
obj = copy_maybe(true, obj);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IFNvbWVPYmplY3QKewoJU29tZU9iamVjdCgpIDogdmFsdWUoMCksIGhhbmRsZSgwKSB7fQoJCglTb21lT2JqZWN0KGNvbnN0IFNvbWVPYmplY3QmIG8pIDogdmFsdWUoby52YWx1ZSksIGhhbmRsZShvLmhhbmRsZSkgewoJCXN0ZDo6Y291dCA8PCAiQ29weSBjb25zdHJ1Y3QgZnJvbSAiIDw8IHRoaXMgPDwgIiB0byAiIDw8ICZvOwoJfQoJCglTb21lT2JqZWN0KFNvbWVPYmplY3QmJiBvKSA6IHZhbHVlKG8udmFsdWUpLCBoYW5kbGUoby5oYW5kbGUpIHsKCQlzdGQ6OmNvdXQgPDwgIk1vdmUgY29uc3RydWN0IGZyb20gIiA8PCB0aGlzIDw8ICIgdG8gIiA8PCAmbzsKCX0KCQoJdm9pZCBvcGVyYXRvcj0oU29tZU9iamVjdCBjb25zdCAmbykKCXsKCQlzdGQ6OmNvdXQgPDwgIkNvcHkgZnJvbSAiIDw8IHRoaXMgPDwgIiB0byAiIDw8ICZvOwoJfQoJdm9pZCBvcGVyYXRvcj0oU29tZU9iamVjdCAmJm8pCgl7CgkJc3RkOjpjb3V0IDw8ICJNb3ZlIGZyb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