12

由于std::unique_ptr提供了一种避免内存泄漏和确保异常安全的便捷方法,因此传递它们而不是原始指针是明智的。因此,一个人可能想要(成员)具有类似签名的函数

std::unique_ptr<some_type> foo(some data);

不幸的是,在实现这样的功能时,不能简单地

std::unique_ptr<some_type> foo(some data)
{
  return { new some_type(data) };                  // error
}

但必须改为

std::unique_ptr<some_type> foo(some data)
{
  return std::move( std::unique_ptr<some_type>( new some_type(data) ) );   // awkward
}

因为构造函数unique_ptr::unique_ptr(pointer)explicit. 这个构造函数背后的原因是explicit什么?

制作构造函数的动机之一explicit是防止意外的隐式类型转换。但是,由于unique_ptr不能按值传递,这应该不是问题,不是吗?

4

2 回答 2

19

unique_ptr获得传递的指针的所有权。取得所有权应该是明确的——你不希望一些指向“神奇地”的指针被某个类拥有(和删除)(这是 deprecated 的问题之一std::auto_ptr)。

例如:

void fun(std::unique_ptr<X> a) { .... }
X x;
fun(&x); // BOOM, deleting object on stack, fortunately it does not compile
fun(std::unique_ptr<X>(&x)); // compiles, but it's explicit and error is clearly visible

请注意,这std::move在语句中不是必需的return(特殊语言例外 - 作为return参数的局部变量可以被视为“已移动”)。

此外 - 在 C++14 中,您可以使用std::make_unique它来减少尴尬:

return std::make_unique<some_data>(some_data_argument1, arg2);

(它也可以很容易地添加到 C++11 -在这里阅读)

于 2015-08-28T11:31:20.767 回答
5

采用唯一 ptr 的参数不应默默地获得指针的所有权。

因此 ctor 是明确的。

要返回,请尝试make_unique<foo>(?)代替{new foo(?)}.

于 2015-08-28T11:33:29.990 回答