Reputation: 8293
I have a object whose copy operation would be too slow so I decided to delete
it and force users to only move. A copy of this object wound't make much sense anyway. But then I have this function:
Object loadFromFile(const std::string& name) {
Object obj;
...
return obj;
}
Even though copy elision happens here and no copy constructor is called, this fails to compile because a copy constructor is required to exist and be accessible. This is my second attempt:
Object&& loadFromFile(const std::string& name) {
Object obj;
...
return std::move(obj);
}
This compiles. Yay!
But a new problem surges when trying to use it:
Object x = loadFromFile("test.txt");
This again requires a copy constructor. I couldn't get it to work even explicitly using move:
Object x = std::move(loadFromFile("test.txt"));
The only solution I came was:
const Object& x = loadFromFile("test.txt");
But x
has to be non-const as it is going to be altered later.
How to deal with it?
Upvotes: 2
Views: 371
Reputation: 2965
This one is wrong:
Object&& loadFromFile(const std::string& name) {
Object obj;
...
return std::move(obj);
}
You are returning a reference to a local variable, and this is undefined behaviour. The object dies and what you return is a reference to your nose, so demons can come out of it.
The second one is right:
Object loadFromFile(const std::string& name) {
Object obj;
...
return obj;
}
Indeed, in this case, lookup is performed first as if obj
was an rvalue (Standard 12.8.32):
When the criteria for elision of a copy/move operation are met, but not for an exception-declaration, and the object to be copied is designated by an lvalue, or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. If the first overload resolution fails or was not performed, or if the type of the first parameter of the selected constructor is not an rvalue reference to the object’s type (possibly cv-qualified), overload resolution is performed again, considering the object as an lvalue. [ Note: This two-stage overload resolution must be performed regardless of whether copy elision will occur. It determines the constructor to be called if elision is not performed, and the selected constructor must be accessible even if the call is elided. —end note ]
And move-constructor Object(Object&&)
should be selected.
Upvotes: 3
Reputation: 8293
Ops, my mistake, sorry.
I deleted the copy constructor, but didn't actually implement a move one assuming it would already be there. Creating a move constructor solves the issue.
Thanks for the light @Joseph Mansfield.
Upvotes: 2
Reputation: 4017
Can you pass it as an output parameter? Something like:
void loadFromFile(const std::string& name, Object& obj) {
//Operations on obj
}
Upvotes: 0