f



Prefix and postfix operation on operator ->

Hi,

I have a smartpointer type, where I want to perform an operation before and
after each dereference. I tried the following:

template < class T >
class sptr
{
public:

    proxy operator -> () { return proxy( mPtr ); }

private:
    class proxy
    {
        proxy( T* t ) : mPtr( t ) { prefix_op(); }
        ~proxy() { postfix_op(); }

        T* operator -> { return mPtr; }
        private:
        T* mPtr;
    };

    T* mPtr
};

This works on MSVC but fails on GCC 2.9.x, because of different lifetimes of
objects. On MSVC the following code:

    sptr< bar > p( ... );
    p->foo();

does the following:

    prefix_op();
    bar::foo();
    postfix_op();

whereas on GCC, the following happens:

    prefix_op();
    postfix_op();
    bar::foo();

which is bad because the postfix shuts something down that is needed in
bar::foo();. I was wondering whether the standard says anything about the
lifetime of the proxy object in the code above. Futhermore, is there a way
to achieve what I am trying to achieve?

Regards,

Jaap Suter


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Jaap
6/28/2003 11:16:40 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

1 Replies
644 Views

Similar Articles

[PageSpeed] 8

Jaap Suter wrote:

> This works on MSVC but fails on GCC 2.9.x, because of different
> lifetimes of objects. On MSVC the following code:
> 
>     sptr< bar > p( ... );
>     p->foo();
> 
> does the following:
> 
>     prefix_op();
>     bar::foo();
>     postfix_op();

Which is the correct behaviour. 

> 
> whereas on GCC, the following happens:
> 
>     prefix_op();
>     postfix_op();
>     bar::foo();

Which is bad, since temporaries shall exist during the lifetime of the
operation they are involved into. This is probably a problem with the
gcc you are using. gcc 3.2.2 here gets it right and I remember that
earlier 2.95.x versions did that right too, because I've used the same
technique to implement handle-to-pointer locking during calls once.  

Regards
Marco
-- 
The text above is a result of a bug in my newsreader and I take no 
responsibility for this text appearing in my post. 


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Marco
6/29/2003 10:17:50 PM
Reply: