加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么static_cast可以编译将指针转换为智能指针

发布时间:2020-12-16 10:14:09 所属栏目:百科 来源:网络整理
导读:我只是偶然发现了一个来自智能指针的演员,想要检查一下static_cast是否可以在编译时声明以下是无意义的: int main(){ char foobar[4] = "Foo"; std::unique_ptrchar[] myptr = static_castdecltype(myptr)(foobar); myptr.reset(); return 0;} 这里发生的是
我只是偶然发现了一个来自智能指针的演员,想要检查一下static_cast是否可以在编译时声明以下是无意义的:

int main()
{
    char foobar[4] = "Foo";
    std::unique_ptr<char[]> myptr = static_cast<decltype(myptr)>(foobar);
    myptr.reset();
    return 0;
}

这里发生的是myptr试图释放foobar.

我不是在问什么是智能指针或如何分配或以其他方式修复上述内容.

我认为这个问题应该在编译时捕获,因为这些类型应该是完全不兼容的.

为什么在编译时没有检测到这个?

解决方法

static_cast会调用std :: unique_ptr的构造函数,类似于以下示例中的内容

#include <iostream>
#include <memory>

using std::cout;
using std::endl;

class Something {
public:
    explicit Something(int) {
        cout << __PRETTY_FUNCTION__ << endl;
    }
};

int main() {
    auto something = static_cast<Something>(1);
    (void) something;
}

如果您想知道为什么static_cast会调用std :: unique_ptr的构造函数,可以用标准中的以下引用来解释(强调我的)

静态演员[expr.static.cast / 4]

An expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed,for some invented temporary variable t (8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary variable as the result of the conversion. The expression e is used as a glvalue if and only if the initialization uses it as a lvalue.

所以基本上在你的例子中,数组被视为unique_ptr的构造函数的参数,然后虚构的临时用于初始化变量myptr(在大多数情况下使用elision)

在您的示例中调用的构造函数是(2)在以下cppreference页面http://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr中

explicit unique_ptr( pointer p ) noexcept;

在这里你得到一个指向数组的unique_ptr.

然后,当您调用reset()时,unique_ptr会尝试使用自动生存期删除该变量,并导致未定义的行为.然而,编译器不需要检测到这一点.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读