gpt4 book ai didi

c++ - shared_ptr 上的 static_cast 导致未定义的行为

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:31 24 4
gpt4 key购买 nike

根据书:The C++ Standard Library: a tutorial and reference 我引用以下内容:

The cast operators allow casting a pointer to a different type. The semantic is the same as the corresponding operators, and the result is another shared pointer of a different type. Note that using the ordinary cast operators is not possible, because it results in undefined behavior

因此,例如,以下代码会产生未定义的行为:

shared_ptr<Base> base_ptr (new Derived);
shared_ptr<Derived>(static_cast<Derived*>(base_ptr.get())); --> Undefined Behaviour.

因此,应该改用static_pointer_cast()

第二个例子:

shared_ptr<void> sp(new int);                // shared pointer holds a void* internally
shared_ptr<int>(static_cast<int*>(sp.get())) // ERROR: undefined behavior

标准确实支持这样的声明吗?

最佳答案

给定:

shared_ptr<Base> base_ptr (new Derived);  // ** see below
shared_ptr<Derived> derived_ptr;

表达式:

Derived* p = static_cast<Derived *>(base_ptr.get());

正确地从基类指针产生一个指向派生类接口(interface)的指针。然而,这是一个原始指针。

这本身不是 UB。

但是,下一个逻辑错误可能是:

derived_ptr.reset(p);

这会产生一个微妙而讨厌的错误,因为您现在有两个不同的 shared_ptr每个都有自己的控制 block (它跟踪受控对象生命周期的方式)。

derived_ptrbase_ptr现在将独立控制 p 指向的对象指针的生命周期。 .

相比之下,

derived_ptr = std::static_pointer_cast<Derived>(base_ptr);

原因 derived_ptrbase_ptr 共享相同的控制 block ,因此,新对象的生命周期将得到正确管理。

请注意,在上述示例中,无论如何您都有 UB。

你只能static_castDerived如果base_ptr 真的是指向一个Derived .在你的情况下它不是。它指向 Base并且再多的转换都不会正确地向下转换它。

我已在上面进行了修改以更正此问题。

关于c++ - shared_ptr 上的 static_cast 导致未定义的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com