Does the standard mandate enable_shared_from_this is to be inherited publicly? Why?

后端 未结 1 1425
逝去的感伤
逝去的感伤 2021-01-18 06:05

It\'s common to inherit from enable_shared_from_this just to be able to return shared_ptr\'s from member functions as the primary intention, with n

相关标签:
1条回答
  • 2021-01-18 06:28

    Since private enable_shared_from_this + friend shared_ptr seems to cover this use case, shouldn't it be sufficient by the standard?

    No. The standard permits implementations wide latitude with how they implement things. The constructor of shared_ptr<T> that adopts an object may defer the shared-from-this stuff to some helper function or other object. For maximum irony, it could defer to a base class of shared_ptr<T> ;)

    As such, enable_shared_from_this must be accessible by any code in order for the shared_ptr constructor to work.

    0 讨论(0)
提交回复
热议问题