c – 为什么static_cast可以编译将指针转换为智能指针
我只是偶然发现了一个来自智能指针的演员,想要检查一下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]
所以基本上在你的例子中,数组被视为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会尝试使用自动生存期删除该变量,并导致未定义的行为.然而,编译器不需要检测到这一点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |