How could one implement an observer_ptr?

喜欢而已 提交于 2019-11-28 04:44:28

问题


I'd like to use observer_ptr in my project, but the paper only defines the interface, not the complete implementation.

Is there an easy way to implement it myself?


回答1:


You can create the so called observer_ptr trivially by creating a unique_ptr with a NOP deleter.

template<typename T>
struct nop_deleter
{
  void operator()(T*) const {}
};

template<typename T>
using observer_ptr = unique_ptr<T, nop_deleter>;

This will still have unique_ptr's behavior, meaning it's move-only, while you'd want observer_ptr to be copyable. Which leads us to a simpler implementation:

template<typename T>
using observer_ptr = T*;

This does everything you want. You can call it observer_ptr<int> instead of int *, because the latter is, of course, evil. It's copyable, and does nothing upon destruction.


I'm being facetious in the answer above, but hopefully, it'll demonstrate that observer_ptr doesn't have much utility other than having a different name than a raw pointer type. There's nothing wrong in using a non-owning raw pointer.

You may argue that observer_ptr conveys intent, but that argument is only valid if your code base contains instances of raw pointers that manage resources. Eliminate those and then a raw pointer will automatically mean observer_ptr ... without the fancy name.


If you absolutely must have the fancy name, and/or a distinct type, implementing it yourself should be easy.



来源:https://stackoverflow.com/questions/24157735/how-could-one-implement-an-observer-ptr

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