0

以下编译并按预期工作:

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;
}

但这不会:

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++ 输出: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>]

从一些 SO 研究中返回 unique_ptr from functions为什么我不能从一对中返回 unique_ptr?似乎这都与复制省略命名返回值优化有关。

有人可以确认我的猜测是否正确。如果是这样,复制 elison的确切标准是什么,以便可以应用 NRVO ?

4

0 回答 0