【发布时间】:2020-10-08 04:44:22
【问题描述】:
我了解大多数标准库实现选择通过在基类中存储std::weak_ptr 来实现std::enable_shared_from_this。这导致以下结果:
#include <memory>
class Foo : public std::enable_shared_from_this<Foo>
{
public:
std::shared_ptr<Foo> GetSharedBar() const
{
// returns shared_ptr<const Foo> instead of std::shared_ptr<Foo>
return shared_from_this();
// Error in VC++2019 due to mismatch.
}
};
我似乎很清楚,尽管需要更新引用计数,shared_from_this() 实际上并没有改变目标对象。这似乎是可变对象的理想用途,因此可以将 shared_from_this() 相对于派生对象标记为 const。
为什么不用mutable实现?
为了阐明下面的答案:它是用标记为mutable 的std::weak_ptr 实现的,但这只允许std::weak_ptr 发生突变,而不是让我们将非const 交给其他人。
【问题讨论】:
-
@Eljay,你在使用 VC++ 吗?我更新了示例以包含重现我的观察所必需的所有内容。
-
这种行为肯定是正确的吗?在
GetSharedBar()中,this是指向constFoo的指针。如果没有一些铸造恶作剧,你不可能将它作为指向非constFoo的指针(包装在shared_ptr<>或其他)中返回。 -
嗯,我的理解是 std::enable_shared_from_this 包含指向 this 的 std::weak_ref,而不是 const this。如果weak_ref 被标记为可变的,我们就不能使用复制构造函数实例化一个指向它的std::shared_ptr 吗?
-
我认为它按预期工作,如果您取出共享指针并尝试将
this作为非常量指针或引用返回,您将得到相同的错误 -
为什么不用mutable实现?控制块是用mutable实现的。问题是方法需要是非常量,或者返回类型需要是
std::shared_ptr<Foo const>,或者你需要return const_cast<Foo*>(this)->shared_from_this()这是janky。
标签: c++ visual-c++ shared-ptr mutable enable-shared-from-this