#pragma once template class CowPtr { public: typedef std::shared_ptr RefPtr; private: mutable RefPtr m_sp; void detach() { if( !( m_sp == nullptr || m_sp.unique() ) ) { m_sp = std::make_shared(*m_sp); } } public: CowPtr() = default; CowPtr(const RefPtr& refptr) : m_sp(refptr) { } bool operator==(std::nullptr_t) const { return m_sp == nullptr; } bool operator!=(std::nullptr_t) const { return m_sp != nullptr; } const T& operator*() const { return *m_sp; } T& operator*() { detach(); return *m_sp; } const T* operator->() const { return m_sp.operator->(); } T* operator->() { detach(); return m_sp.operator->(); } };