Boost logo

Boost :

From: Giovanni Bajo (giovannibajo_at_[hidden])
Date: 2002-07-09 13:01:38


----- Original Message -----
From: "David B. Held" <dheld_at_[hidden]>
Newsgroups: gmane.comp.lib.boost.devel
To: <boost_at_[hidden]>
Sent: Tuesday, July 09, 2002 7:13 PM
Subject: [boost] Re: Re: Re: Re: shared_ptr and ->*

> Is it possible to create
> a global operator->* that takes a shared_ptr, and calls bind()?

Not with the C++ I know, but maybe I just don't know how :)

> I guess you would need a return value then, which is probably why we
> have bind(), instead of shared_ptr::operator->*. Oh well.

I believe that you're still missing my main point against the need of a
operator->* (which I still agree it could be added, but I think it's
basically useless). The point is that you shouldn't want an operator->* in
shared_ptr in the first place, becuase you shouldn't be using (as in:
storing) a pointer-to-member-function in the first place. Boost
(bind+function) provides very powerful, elegant, and more generic
alternatives, that do not require overloading of ->* at all.

For example:

---------------------------------
class FSM
{
public:
    void State1(int parm);
    void State2(int parm);
    void State3(int parm);
};

// Old-skool implementation
void (FSM::*curState)(int );

curState = &FSM::StateX;
[...]
(objPtr->*curState)(value);

// Boost implementation:
boost::function<void, int> curState;

curState = boost::bind(&FSM::StateX, objPtr, _1);
[...]
curState(value);
---------------------------------

Now, I might well be wrong, but I'd like to see a real-word example of a
codebase that can't be converted to such code (which also gives some
structural advantages).
Actually, the only utility I see in adding the ->* operator is that it would
help minimizing the efforts in porting an existing codebase from dump
pointers to shared_ptr.

Giovanni Bajo


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