为什么在返回指针时不能将其自动转换为unique_ptr? [英] Why can't a pointer be automatically converted into a unique_ptr when returning it?

查看:163
本文介绍了为什么在返回指针时不能将其自动转换为unique_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我通过一个例子提出我的问题.

Let me pose my question through an example.

#include <memory>

std::unique_ptr<int> get_it() {
        auto p = new int;
        return p;
}

int main() {
        auto up ( get_it() );
        return 0;
}

这无法编译并出现以下错误:

This fails to compile with the following error:

a.cpp:5:9: error: could not convert ‘p’ from ‘int*’ to ‘std::unique_ptr<int>’
  return p;
         ^

为什么这里没有从原始指针到唯一指针的自动转换?而我应该怎么做呢?

Why isn't there an automatic conversion from a raw pointer to a unique one here? And what should I be doing instead?

动机:我知道使用智能指针明确所有权应该是一种好习惯;在这种情况下,我从某个地方得到一个指针(我拥有),作为int*,我(认为我)希望在unique_ptr中使用它.

Motivation: I understand it's supposed to be good practice to use smart pointers for ownership to be clear; I'm getting a pointer (which I own) from somewhere, as an int* in this case, and I (think I) want it in a unique_ptr.

如果您正在考虑发表评论或添加自己的答案,请访问赫伯特·萨特(Herbert Sutter)的论点在提案N4029中是可能的.

If you're considering commenting or adding your own answer, please address Herbert Sutter's arguments for this to be possible in proposal N4029.

推荐答案

答案是双重的.其他所有答案(包括OP的自我答案)仅解决了一半.

The answer is two-fold. All the other answers, including a OP's self-answer, addressed only one half of it.

不能自动转换指针,因为:

The pointer cannot be automatically converted because:

  • unique_ptr构造函数声明为explicit,因此认为由编译器仅在显式上下文中使用.这样做是为了防止意外的危险转换,在这种转换中unique_ptr可能会劫持指针,并在程序员不知情的情况下将其删除.通常,不仅对于unique_ptr,将所有单参数构造函数声明为explicit也是一种好习惯,以防止意外转换.
  • 标准将
  • return语句视为隐式上下文,因此显式构造函数不适用. EWG问题114 中反映了该决定是否正确的持续讨论.几个提案:由Herb Sutter明确显示return的两个提案版本( N4029 N4074 )和两个响应",坚持不这样做: N4094 (作者:Howard Hinnant和Ville Voutilainen), FilipRoséen的 N4131 .经过多次讨论和民意测验,该问题已作为NAD结束-不是缺陷.
  • unique_ptr's constructor from a pointer is declared explicit, thus considered by the compiler only in explicit contexts. This is done so to prevent accidental dangerous conversions, where a unique_ptr can hijack a pointer and delete it without programmer's knowledge. In general, not only for unique_ptr, it is considered a good practice to declare all single-argument constructors as explicit to prevent accidental conversions.
  • return statement is considered by the standard an implicit context, and thus the explicit constructor is not applicable. There was an ongoing discussion if this decision is right, reflected in EWG issue 114, including links to several proposals: two versions of proposals to make return explicit by Herb Sutter (N4029, N4074), and two "responses", arguing not to do so: N4094 by Howard Hinnant and Ville Voutilainen and N4131 by Filip Roséen. After several discussions and polls the issue was closed as NAD - not a defect.

当前,有几种解决方法:

Currently, there are several workarounds:

return std::unique_ptr<int>{p};

return std::unique_ptr<int>(p);

在c ++ 14中,您还可以使用自动推论函数的返回类型:

In c++14, you can use auto-deduction of function return type as well:

auto get_it() {
    auto p = new int;
    return std::unique_ptr<int>(p);
}

更新:第二点添加了指向委员会问题的链接.

Update: added a link to committee issue for the second point.

这篇关于为什么在返回指针时不能将其自动转换为unique_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆