问题描述
我只是可以使用C ++使用bcp提取 boost :: shared_ptr
文件,这太重了。
我想要什么将是一个简单的智能指针实现 boost :: shared_ptr
,但没有全部 boost
开销(如果可能的话) 。)
我可以想出自己的版本阅读增强源,但我担心丢失一个或多个点,似乎很容易造成错误的智能指针,我可以
所以,做一个简单的实现或实现例子 boost :: shared_ptr $ c存在可以使用或可以作为灵感的$ c>(或任何引用计数等效智能指针)?
如果您不需要混合共享和 ,并且不需要自定义删除,您只需使用快速且脏的my_shared_ptr:
模板< class T>
class my_shared_ptr
{
template< class U>
朋友类my_shared_ptr;
public:
my_shared_ptr():p(),c(){}
显式my_shared_ptr(T * s):p(s),c(new unsigned(1)){}
my_shared_ptr(const my_shared_ptr& s):p(sp),c(sc){if(c)++ * c; }
my_shared_ptr& operator =(const my_shared_ptr& s)
{if(this!=& s){clear(); P = s.p; C =皮下;如果(c)++ * c; } return * this; }
template< class U>
my_shared_ptr(const my_shared_ptr< U>& s):p(s.p),c(s.c){if(c)++ * c; }
〜my_shared_ptr(){clear(); }
void clear()
{
if(c)
{
if(* c == 1)delete p;
if(! - * c)delete c;
}
c = 0; P = 0;
}
T * get()const {return(c)? p:0; }
T * operator->()const {return get(); }
T& operator *()const {return * get(); }
private:
T * p;
unsigned * c;
}
code> make_my_shared< X> ,它可以简单地实现为
template< class T,class ... U>
auto make_my_shared(U&& ... u)
{
return my_shared_ptr< T>(new T {std :: forward< U>(u)...});
}
被调用为
auto pt = make_my_shared< T>(...);
I'm using C++ in an embedded linux environment which has GCC version 2.95
.
I just can't extract boost::shared_ptr
files with bcp, it is just too heavy.
What I'd like would be a simple smart pointer implementation of boost::shared_ptr
but without all boost
overheads (if it is possible...).
I could come up with my own version reading boost source but I fear missing one or more points, it seems easy to make a faulty smart pointer and I can't afford to have a buggy implementation.
So, does a "simple" implementation or implementation example of boost::shared_ptr
(or any reference counting equivalent smart pointer) exists that I could use or that I could take as an inspiration?
if you don't need mixing shared and weak ptr,and don't need coustom deletors, you can just use the quick and dirty my_shared_ptr:
template<class T>
class my_shared_ptr
{
template<class U>
friend class my_shared_ptr;
public:
my_shared_ptr() :p(), c() {}
explicit my_shared_ptr(T* s) :p(s), c(new unsigned(1)) {}
my_shared_ptr(const my_shared_ptr& s) :p(s.p), c(s.c) { if(c) ++*c; }
my_shared_ptr& operator=(const my_shared_ptr& s)
{ if(this!=&s) { clear(); p=s.p; c=s.c; if(c) ++*c; } return *this; }
template<class U>
my_shared_ptr(const my_shared_ptr<U>& s) :p(s.p), c(s.c) { if(c) ++*c; }
~my_shared_ptr() { clear(); }
void clear()
{
if(c)
{
if(*c==1) delete p;
if(!--*c) delete c;
}
c=0; p=0;
}
T* get() const { return (c)? p: 0; }
T* operator->() const { return get(); }
T& operator*() const { return *get(); }
private:
T* p;
unsigned* c;
}
For anyone interested in make_my_shared<X>
, it can be trivially implemented as
template<class T, class... U>
auto make_my_shared(U&&... u)
{
return my_shared_ptr<T>(new T{std::forward<U>(u)...});
}
to be called as
auto pt = make_my_shared<T>( ... );
这篇关于在嵌入式环境中boost :: shared_ptr的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!