I've noticed that newer libraries have been deleting the copy constructors from their objects. These objects always require a bit of build-up, so I inevitably have them returned by a function.
But does this mean I'm expected to use pointer semantics after retrieving the object?
Example:
This won't work because the library's object has a deleted copy constructor.
#include <memory>
//fancy library object
struct Foo{
Foo(){}
Foo(Foo const& foo)=delete;
};
Foo Create_Foo(){
Foo f;
// ... customize f before returning ...
return f;
}
int main(){
auto f = Create_Foo();
}
It doesn't seem like I can move the object out of the function:
Foo&& Create_Foo(){
Foo f;
// ... customize f before returning ...
return std::move(f);
}
So I have no choice but to use pointer semantics now?
std::unique_ptr<Foo> Create_Foo(){
auto f = std::make_unique<Foo>();
// ... customize f before returning ...
return f;
}
Is there any way to avoid using pointers,
but still get the constructed object as the result of the function?
I'm not apposed to using pointers, as it's likely the efficient and correct thing to do, but I'm interested in knowing if this is something I'm forced to do when I want the constructed object as the result of the function.