- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想了解为什么在调用 shared_from_this
时会出现 bad_weak_ptr
异常。
#include <memory>
#include <iostream>
class parent : public std::enable_shared_from_this<parent>
{
public:
void compare(std::shared_ptr<parent> const& p2)
{
std::cout << (this->shared_from_this() == p2->shared_from_this());
}
};
class child1 : public parent
{};
class child2 : public parent
{};
class child3 : public child1, public child2
{};
void compare(parent& p1, parent& p2)
{
std::cout << &p1 << " : " << &p2 << "\n";
std::cout << (&p1 == &p2);
}
void compare(std::shared_ptr<parent> const& p1, std::shared_ptr<parent> const& p2)
{
compare(*p1, *p2);
// p1->compare(p2); // bad_weak_ptr
// auto p = p1->shared_from_this(); // bad_weak_ptr
}
void compareusingchild(std::shared_ptr<child1> const& c1, std::shared_ptr<child2> const& c2)
{
compare(c1, c2);
}
int main()
{
std::shared_ptr<child3> c3 = std::make_shared<child3>();
try
{
compareusingchild(c3, c3);
}
catch (std::exception& e)
{
std::cout << e.what();
}
return 0;
}
我发现通过使 class parent
继承虚拟化,这个问题似乎不会持续存在。为什么这不是编译时错误?当找不到正确的继承父级时,类似于“模棱两可的函数调用”?
仅包含父类的 API 无法预先知道继承层次结构,调用比较方法(在父类中)将导致运行时错误。是否有可能使编译时检测到此类错误?
最佳答案
好的,现在我知道问题出在哪里了。
菱形继承(钻石问题)禁用shared_from_this()
.
在引擎盖下(对于 MSVC 2017)你可以找到这样的东西:
template<class _Yty,
class = void>
struct _Can_enable_shared
: false_type
{ // detect unambiguous and accessible inheritance from enable_shared_from_this
};
template<class _Yty>
struct _Can_enable_shared<_Yty, void_t<typename _Yty::_Esft_type>>
: is_convertible<remove_cv_t<_Yty> *, typename _Yty::_Esft_type *>::type
{ // is_convertible is necessary to verify unambiguous inheritance
};
所以基本上当生成模板时,它会检查是否从 child3 *
转换而来至 std::enable_shared_from_this<parent> *
是可能的。如果可能,则设置内部弱指针,否则什么都不做。
现在由于存在歧义,简单的转换是不可能的 std::is_convertible
返回 false 和 shared_from_this
未启用(设置为适当的值)。
这是一个证明:https://godbolt.org/z/V2AzLk
std::cout << "Conv child3: " << std::is_convertible<child3*, std::enable_shared_from_this<parent>*>::value << std::endl;
std::cout << "Conv child2: " << std::is_convertible<child2*, std::enable_shared_from_this<parent>*>::value << std::endl;
std::cout << "Conv child1: " << std::is_convertible<child1*, std::enable_shared_from_this<parent>*>::value << std::endl;
打印:
Conv child3: 0
Conv child2: 1
Conv child1: 1
所以基本上歧义不会导致编译问题,它只是不会启用此功能。
关于c++ - 多重继承调用 shared_from_this 导致的 Bad_weak_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706912/
我正在学习智能指针和shared_from_this。在Class Inheritance Relations中,会很难理解。 我有两个基类CA和CB,它们派生自enable_shared_from_
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: enable_shared_from_this - empty internal weak pointer?
我想创建一些 Timer 类,它每 N 秒打印一次“文本”,其中 N 将在构造函数中初始化。 #include #include #include #include class Timer :
我使用 boost asio 和 beast(用于浏览器支持)开发桌面聊天。 我使用这个架构: 但是,在构建时,我遇到了一个问题:bad_weak_ptr,我不知道哪里出了问题:s这是源代码的链接 h
我试图将一个共享指针从 QTGUI 类传递到 Client 类,但一直收到 bad weak ptr 错误。我读到我不能直接在构造函数中分配 shared_from_this(),因为此时指针还没有准
当我这样做时出现异常:std::bad_weak_ptr->shared_from_this() template class painter_record_t { ....... private:
我正在尝试在 asio 中保留已连接客户端的列表。我已经从文档 (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cp
我想了解为什么在调用 shared_from_this 时会出现 bad_weak_ptr 异常。 #include #include class parent : public std::ena
我开发了一些可以正确编译但在(调试)运行时失败的代码。我正在使用 VS2015。 背景:我正在构建一个高级消息引擎。为了使新消息的编程添加可维护,在生产代码中,我花时间使用 explicit init
我有一个 SuperParent 类,一个 Parent 类(派生自 SuperParent)并且都包含一个 shared_ptr到一个 Child 类(它包含一个 weak_ptr 到一个 Supe
为了创建我的 EventManager,我需要创建一些函数,这些函数会使用 Listeners 的 shared_ptr 将它们存储到 vector 中并调用它们的事件函数。我这样做了,它工作正常,除
我正在将 BOOST 移植到我的 TI 嵌入式平台,在嵌入式环境中不支持异常是很常见的,所以我的 TI 嵌入式平台支持。所以我使用 BOOST_EXCEPTION_DISABLE 和 BOOST_NO
我有一个基类和一个派生类。 API 仅公开 Derived,而实现隐藏在 Base 中(gcc 属性 visibility 设置为 hidden ),因为一些内部 API 模块必须调用 Base 方法
编辑:我从来没有弄明白这一点——我重构了代码,使其与 Boost 示例几乎相同,但问题仍然存在。如果其他人遇到此问题,则您的问题可能是更常见的 shared_from_this() 在不存在 shar
我定义了一个类似于下面的类: class A : std::enable_shared_from_this { public: static std::shared_ptr create() {
我试图理解 std::enable_shared_from_this 类的行为,但我无法理解。所以我写了一个简单的程序来测试不同的情况。 问题 谁能解释一下下面代码的行为,因为我无法解释观察到的结果。
我正在使用独立的 Asio 和 C++11 创建一个 C++ 服务器应用程序,但遇到错误,这就是我寻求帮助的原因。 错误 在类里面worker_thread , 在通话期间 shared_from_t
首先,我已阅读列出的所有相关问题。 他们说,“在使用 shared_from_this 之前,你必须有一个指向 this 的现有 shared_ptr。”据我所知,我绝不会违反该条件。我将 Foo 的
首先,这个问题似乎与同时使用 clang(任何版本)和高于 6.5.0 版本的 libstdc++ 有关。 我在我的代码库中使用以下习惯用法来对用户隐藏实现: #include class mycl
这个问题在这里已经有了答案: std::enable_shared_from_this: is it allowed to call shared_from_this() in destructor
我是一名优秀的程序员,十分优秀!