Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

陌路散爱 提交于 2019-12-05 08:07:05

I can't find anything in the standard (well, the final draft) that specifically rules it out. The closest I can find is a note in 20.9.11.2.10 shared_ptr casts

5 [ Note: The seemingly equivalent expression shared_ptr(static_cast(r.get())) will eventually result in undefined behavior, attempting to delete the same object twice. —end note ]

which actually seems to forget about your case with a custom deleter.

If the first shared_ptr object is destroyed, then you get UB because objects using the second might access released object.

Since you made sure that your first shared_ptr object lives longer then the second, you do not get UB.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!