I am attempting to delete the copy constructor using the c++ type system to prevent copying an object.
struct DeleteCopyConstructor {
DeleteCopyConstructor() {};
DeleteCopyConstructor(DeleteCopyConstructor& op2) = delete;
DeleteCopyConstructor(const DeleteCopyConstructor& op2) = delete;
};
DeleteCopyConstructor f() {
DeleteCopyConstructor d;
// initialize d...
return d;
}
The error is:
error: use of deleted function ‘DeleteCopyConstructor::DeleteCopyConstructor(const DeleteCopyConstructor&)’
I've read about copy elision, but it appears to be a compiler optimization, so I don't think it applies. How can I return d
without triggering copy construction?
delete
two copy constructors. – TombstoneDeleteCopyConstructor(DeleteCopyConstructor&& op2) = default;
– Tomchaychar[sizeof DeleteCopyConstructor]
variable on the stack of the caller, pass the pointer tof
as an out variable, and use a placement new. – Tomchay