Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-02-14 20:04:35


Mark Deric wrote:
> "Peter Dimov" <pdimov_at_[hidden]> wrote:
>
>>> struct Dummy { int a; }
>>>
>>> class Lock {
>>> private:
>>> Dummy &_d;
>>> public:
>>> Lock( Dummy &d ) : _d(d) { std::cout << "Locked." << std::endl; }
>>> ~Lock( void ) { std::cout << "Unlocked." << std::endl; }
>>>
>>> Dummy *operator->( void ) { return &_d; }
>>> };
>>>
>>> class Ref {
>>> private:
>>> Dummy &_d;
>>> public:
>>> Ref( Dummy &d ) : _d(d) { }
>>>
>>> Lock operator->(void) { return Lock(_d); }
>>> };
>
>> Your code is non-portable. You are assuming that the temporary
>> Lock(_d) will be constructed directly in the return value, but this
>> is not guaranteed.
>
> I'm not sure I'm following "constructed directly in the return value";
> but, it will be constructed before the call to Lock's Dummy
> *operator->(
> void ) as implied by x->a, below. Isn't this all that is required?

No, because ~Lock will be called twice.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk