In some usecases i want to use the unique_ptr as the function return, in order to transfer the ownership of function return to the function caller. Should i use unique_ptr as function return at all? If not, in which usecases should i use the unique_ptr as function return?
An example to explain my question detailedly:
unique_ptr<int> foo() {
unique_ptr<int> rtn(0);
*rtn = 10U;
return std::move(rtn); // 1
// return rtn; // 2
// return rtn; // 3
}
void main() {
unique_ptr<int> ptr = foo();// 1
// unique_ptr<int> ptr = std::move(foo()); //2
// unique_ptr<int> ptr = foo(); //3
}
In this example i just want to give the ownership of returned unique_ptr away from function foo() to caller. With the unique_ptr as function return, i want to tell the function caller, if you get the return value of this function, you must also take the responsibility to release it.
For this usecase should I use the unique_ptr at all? if yes, whcich variant from 1 to 3 should i use? If not, in which usecase should I use the unique_ptr as function return?