|
Boost : |
From: me22 (me22.ca_at_[hidden])
Date: 2006-06-12 21:24:03
On 6/12/06, Pierre THIERRY <nowhere.man_at_[hidden]> wrote:
> I understand, but the following code would fail the same way without
> compilation warnings:
>
> int p* = new int;
> shared_ptr<int> sp;
> sp.reset(p);
> sp.reset(p);
>
But in that case you're typing reset, which means you're aware that
you're using smart pointers.
You also would never have "sp.reset(p);" if sp used to be a plain
pointer, so it's not a problem when refitting existing code to use
smart pointers.
> But this also renders some uses impossible, like this one:
>
> stack<foo*> fooS;
> while(shared_ptr<foo> = fooS.pop())
> {
> ...
> }
>
I'm not sure what that code's trying to do.
Each of these works fine:
stack<foo*> fooS;
shared_ptr<foo> fptr;
while ( fptr.reset(fooS.pop()) ) { ... }
stack<foo*> fooS;
shared_ptr<foo> fptr;
while ( fptr = shared_ptr<foo>(fooS.pop()) ) { ... }
stack<foo*> fooS;
shared_ptr<foo> fptr;
template <typename T>
shared_ptr<T> make_shared_ptr(T *ptr) { return shared_ptr<foo>(ptr); }
while ( fptr = make_shared_ptr(fooS.pop()) ) { ... }
stack<foo*> fooS;
if ( shared_ptr<foo> fptr( fooS.pop() ) ) { ... }
> Sorry for answering so late to this (now) old thread. If I should start
> it again for clarity, just tell me.
>
Luckily gmail still managed to thread it for me =)
HTH,
~ Scott McMurray
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk