Returns a local value from a function without running the copy constructor

I am trying to remove a 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;
}

      

Mistake:

error: use of deleted functionDeleteCopyConstructor::DeleteCopyConstructor(const DeleteCopyConstructor&)’

      

I read about copying, but it seems to be a compiler optimization, so I don't think it applies. How can I revert d

without starting a build copy?

+3


source to share


1 answer


Copy the elision, but it seems to be a compiler optimization, so I don't think it applies.

This is true. More in Move or Named Return Value Optimization (NRVO)?



How can I revert d

without starting a build copy?

Let the compiler take care of this.

+1


source







All Articles