在使用std::shared_ptr时,我有点想念一个shared_ref实现。这是shared_ptr的一个特化版本,它保证永远不会包装一个nullptr(当然,前提是正确使用)。
我好奇为什么它没有出现在C++11标准中。是否有任何重大问题需要解决以实现它?在我脑海中,我想不出任何问题。
编辑:
我希望有一个类似于以下接口:
我好奇为什么它没有出现在C++11标准中。是否有任何重大问题需要解决以实现它?在我脑海中,我想不出任何问题。
编辑:
我希望有一个类似于以下接口:
template <typename T>
class shared_ref {
public:
shared_ref( T&& ref );
T& get();
T* operator&() const;
template< class Y >
void reset( Y&& obj );
long use_count() const;
bool unique() const;
void swap( shared_ref& r );
};
shared_ptr
将会是nullptr
。 - abergmeiershared_ptr
,并不能保证它不是空指针。如果你真的想写出正确的代码,在使用指针之前必须检查是否为 nullptr。但鉴于 95% 的程序员并不关心这一点。 - abergmeiershared_ref
的原因! - abergmeier