The following compiles and works as expected:
std::unique_ptr<char> input_to_char_array()
{
std::unique_ptr<char> c;
c.reset(new char('b'));
// c[1].reset(new char[20]());
return c;
}
But this doesn't:
std::unique_ptr<char> input_to_char_array()
{
std::unique_ptr<char> c[2];
c[0].reset(new char('b'));
c[1].reset(new char('a'));
return c[0]; // is this considered "return statement's expression is the name of a non-volatile object with automatic storage duration"
}
g++ outputs:error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = char; _Dp = std::default_delete<char>]
From some SO research Returning unique_ptr from functions and Why can't I return a unique_ptr from a pair? it seems that this is all related to copy elision and named return value optimization.
Can someone confirm if my guess is correct. If so, what exactly is the criteria for copy elison so that NRVO can be applied?