`
zhangyafei_kimi
  • 浏览: 265630 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

A wrong usage of boost.enable_shared_from_this

阅读更多
A wrong program in boost-user mail list:

#include <iostream>
#include <memory>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

// Inheritance Layout
//
// boost::enable_shared_from_this<T>
//   ^
//   |
// Base --------> IBase
//   ^
//   |
// Derived -----> IDerived


class IBase
{
public:
	virtual ~IBase(){}
	virtual void hello(void) = 0;
};

class Base : public IBase, public boost::enable_shared_from_this<Base>
{
public:
	~Base(void){}

	void hello(void)
	{
		std::cout << "Hello" << std::endl;
	}
};

class IDerived
{
public:
	virtual boost::shared_ptr<Base> root(void) = 0;
	virtual boost::shared_ptr<IBase> root2(void) = 0;
};

class Derived : public IDerived, public Base
{
public:
	~Derived(void){}

	virtual boost::shared_ptr<Base> root(void)
	{
		return shared_from_this();
	}

	virtual boost::shared_ptr<IBase> root2(void)
	{
		return shared_from_this();
	}
};

int main(int argc, char* argv[])
{
	boost::shared_ptr<IDerived> c(new Derived());
	boost::shared_ptr<IBase> d = c->root2();
	boost::shared_ptr<Base> e = c->root();
	std::cout << c.use_count() << std::endl;


	boost::shared_ptr<IDerived> l ((IDerived*)new Derived);
	boost::shared_ptr<Base> n = l->root();//here throws an exception
	boost::shared_ptr<IBase> m = l->root2();//also here
	
	std::cout << m.use_count() << std::endl;

	return 0;
}



Totally, For enable_shared_from_this to work, the static type of the pointer passed to the shared_ptr constructor must inherit from enable_shared_from_this.




shared_ptr's constructor in line 168 calls 'boost::detail::sp_enable_shared_from_this( pn, p, p );'.

'boost::shared_ptr<IDerived> c(new Derived());' is dispatched to 'sp_enable_shared_from_this' in line 86. pay attension to the second parameter, it can accept all types that inherit 'enable_shared_from_this<T>', so does 'Derived*'.

While, 'boost::shared_ptr<IDerived> c((IDerived*)new Derived());' is dispatched to 'sp_enable_shared_from_this' in line 111, bacause the static type IDerived* has nothing to do with 'enable_shared_from_this<T>' and can not be implicitly converted to 'enable_shared_from_this<T>'.

Then check out the difference between the two 'sp_enable_shared_from_this'. It is obvious that the second usage shall cause a BOOST_ASSERT failed in 'enable_shared_from_this::shared_from_this', so an exception is raised.

However, I think the root reason is not the language nor boost, but the design of class IDerived. Its root and root1 seem a little bit irrational because there is not direct relationship between IDerived and Base(IBase).
分享到:
评论

相关推荐

    libxml2-2.9.1-6.el7.5.x86_64.rpm

    yum install 提示ImportError: libxml2.so.2: cannot open shared object file: No such file or directory,需要安装libxml2.so.2

    Making Wrong Code Look Wrong.rar_编程

    在编程领域,编写正确、可读性强的代码是至关重要的,因为这直接影响到代码的维护性和团队合作的效率。...阅读 "Making Wrong Code Look Wrong.pdf" 这份文档,你将深入理解如何将这一理念融入日常编程实践中。

    SWD Debug Interfaces.zip_JTAG/SWD 协议_SWD 协议_jtag_swd_swd协议

    **SWD Debug Interfaces详解** 在嵌入式系统开发中,调试接口是不可或缺的一部分,它使得开发者能够对微控制器(MCU)或系统级芯片(SoC)进行程序加载、调试和故障排查。JTAG(Joint Test Action Group)和SWD...

    CRC_Wrong_Dec_1.rar_CRC MATLAB_crc_crc matlab code

    在"CRC_Wrong_Dec_1.rar"这个压缩包中,主要探讨的是CRC校验在解码时可能出现的错误情况。描述中提到,当data(数据)大于poly(多项式),可能会导致错误的验证code(校验码)。这通常是因为CRC计算过程中对数据...

    EurekaLog_7.5.0.0_Enterprise

    7)....Added: Streaming unpacked debug info into temporal files instead of memory - this greatly reduces run-time application memory usage at cost of slightly slower exception processing. This also ...

    javacard applet 开发实例 正常运行 带jar包

    i= JZSystem.readWriteMifare(JZSystem.MIFARE_PASSWORD_READ, buf, (short)0, (short)(AmbiqConstant.BLOCKS_PER_SECTOR*AmbiqConstant.SECTOR_NUM_OF_8+AmbiqConstant.BLOCK_NUM_OF_0)); if(i==0) ...

    SQL_Assistant_9.5.469_Enterprise_Edition 破解

    This is is a consolidated bug fix release containing the following fixes SA0033354 - Import from Excel: integer values can change to random numbers. SA0033310 - Cloned connections don't always ...

    rand03.6.rar_33 bus ieee_DG on matlab_GA DG_optimal bus_optimal

    The sizing and placement of DGs in the system must be optimal because a wrong choice has a negative impact on the system behavior. To solve this combinatorial problem, an algorithm known as Firefly ...

    Test_f1.rar_magic

    描述中提到 "check that .dex with wrong magic is rejected",这意味着测试的目标是确保系统或工具能够识别并拒绝具有无效魔数(magic number)的.dex文件。这是安全性和正确性的重要方面,因为错误的魔数可能指示...

    cnv.zip_The Just_dali

    1. I have also problem with wrong addressing of lamps. It means two lamps have the same address but on tree in Dali Console I see just one lamp. When I set value for

    drive-plate-wrong-program-processing.rar_液晶电视_电视板

    液晶电视的驱动板是电视内部的关键组件,负责处理图像信号并将其转化为屏幕可显示的格式。在本案例中,我们关注的问题是驱动板(GM2621板)被刷入了错误的程序,导致电视无法正常工作。下面将详细阐述驱动板程序错误...

    SPI_test_SPI实验.zip_STM32F103_shallowlxc_speedw1l_valley899_wrong

    描述中的关键词“shallowlxc”、“speedw1l”、“valley899”和“wrong8zi”可能是实验过程中遇到的不同条件、设置或问题的代号。 STM32F103是意法半导体(STMicroelectronics)生产的一款基于ARM Cortex-M3内核的...

    xfs_dir2_leaf.rar_pop

    描述中提到的"Pop an assert if something is wrong"意味着这是一个用于调试的机制,当检测到错误时会触发断言。 首先,我们需要了解XFS的目录结构。XFS使用B+树(B-tree)来组织目录项,这提高了查找和操作目录的...

    [欧特克数字绘画设计软件].AUTODESK_SKETCHBOOK_DESIGNER_V2012_WIN32-XFORCE

    - b) Click on Activate and it will tell you that your serial is wrong, simply click on close et click on activate again. Choose option a or b. 6. Select I have an activation code from Autodesk 7...

    高中英语 Unit 5 Music课前预习+小组合作探究 新人教版必修2

    __a big hit__ (大热门,非常成功):形容某个事物受到大众热烈欢迎。 - 13. __go wrong__ (出错,失败):事情没有按照预期发展。 - 14. __give a performance__ (演出,表演):在舞台上展示才艺。 - 15. _...

    pda.rar_PDA

    在描述中,“pda do myself access and no wrong”可能指的是用户可以自行操作PDA,并且没有遇到错误,这可能意味着文件中包含了关于如何正确使用和自定义PDA的教程或指南。 在标签 "pda" 中,我们可以推测压缩包的...

    FastReport_6.6.15_VCL_Enterprise_Sources.rar

    - [Lazarus] Fixed bug with preview PageCache wich causes wrong behavior in interactive reports * Interactive charts fix with zoom in preview * Added New "What's new" report * Improved few demo reports...

    allclasses-frame.rar_Not Me

    I am a student. Therefore, I cannot write full source code. Please, I have my source is wrong or not full. So, you give me many kindness.

    Halcon Project.rar_HALCON 截取区域_halcon 兴趣_光条_光条提取_区域开口 halcon

    在本项目中,我们主要探讨的是使用HALCON这一强大的机器视觉软件进行图像处理,特别是针对光条的检测和提取。HALCON是由MVTec公司开发的一款广泛应用在工业自动化中的计算机视觉软件,它提供了丰富的形状匹配、模板...

Global site tag (gtag.js) - Google Analytics