mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-02 14:14:27 +02:00
Fixed a copy/assignment issue.
[SVN r18122]
This commit is contained in:
@@ -24,6 +24,25 @@ namespace boost
|
|||||||
|
|
||||||
template<class T> class enable_shared_from_this
|
template<class T> class enable_shared_from_this
|
||||||
{
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
|
enable_shared_from_this()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_shared_from_this(enable_shared_from_this const &)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_shared_from_this & operator=(enable_shared_from_this const &)
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
~enable_shared_from_this()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
shared_ptr<T> shared_from_this()
|
shared_ptr<T> shared_from_this()
|
||||||
|
@@ -67,18 +67,20 @@ void test()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void test2();
|
void test2();
|
||||||
|
void test3();
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
test();
|
test();
|
||||||
test2();
|
test2();
|
||||||
|
test3();
|
||||||
return boost::report_errors();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
// virtual inheritance from Y to stress the implementation
|
// virtual inheritance to stress the implementation
|
||||||
// (prevents Y* -> impl* casts)
|
// (prevents Y* -> impl*, enable_shared_from_this<impl>* -> impl* casts)
|
||||||
|
|
||||||
class impl: public X, public virtual Y, public boost::enable_shared_from_this<impl>
|
class impl: public X, public virtual Y, public virtual boost::enable_shared_from_this<impl>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -110,3 +112,41 @@ void test2()
|
|||||||
{
|
{
|
||||||
boost::shared_ptr<Y> pi(static_cast<impl2*>(0));
|
boost::shared_ptr<Y> pi(static_cast<impl2*>(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
struct V: public boost::enable_shared_from_this<V>
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
void test3()
|
||||||
|
{
|
||||||
|
boost::shared_ptr<V> p(new V);
|
||||||
|
|
||||||
|
boost::shared_ptr<V> q = p->shared_from_this();
|
||||||
|
BOOST_TEST(p == q);
|
||||||
|
BOOST_TEST(!(p < q) && !(q < p));
|
||||||
|
|
||||||
|
V v2(*p);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
boost::shared_ptr<V> r = v2.shared_from_this();
|
||||||
|
BOOST_ERROR("v2.shared_from_this() failed to throw");
|
||||||
|
}
|
||||||
|
catch(boost::bad_weak_ptr const &)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
*p = V();
|
||||||
|
boost::shared_ptr<V> r = p->shared_from_this();
|
||||||
|
BOOST_TEST(p == r);
|
||||||
|
BOOST_TEST(!(p < r) && !(r < p));
|
||||||
|
}
|
||||||
|
catch(boost::bad_weak_ptr const &)
|
||||||
|
{
|
||||||
|
BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user