#include <memory>
#include <string>
int main()
{
auto foo = std::make_unique<std::string>("hello");
auto bar = foo; // deleted copy !!
return 0;
}
I understand why that code does not compile: I cannot copy a unique_ptr; this is the whole point of being unique.
Question: can I give a small name/reference/alias to foo ?
USE CASE
In my real live, I have a nested structure like std::vector<std::unique_ptr<MyClass>>.
Let X be such an object. I want to work with X[0] and call myMethod; I do
X[0]->myMethod;
Now for clarity, I want to give a (temporary) name to X[0], for example: first. This is why I want to do auto first = X[0].
Is there a way ? Maybe a weak pointer ? I'm not fluent in c++ ;)
EDIT: Not a duplication of this one. Even if the answer is the same, the spirit of the question differs. The linked question is a precise theoretical question while here I ask for a way to define a "shorthand" for a variable by whatever mean.