Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-08-16 10:36:59

From: "John Hunter" <jdhunter_at_[hidden]>
> I have a container of shared pointers and I want to call a member
> function which returns a value and place that return value into
> another container using transform
> class Neuron {
> public:
> ...
> const Train<float_t>* get_ptr_train() const { return &st; }
> ....
> };
> std::vector<boost::shared_ptr<Neuron> nrns;
> std::vector<boost::shared_ptr<const Train<float_t> > trains;
> ...
> std::transform(nrns.begin(), nrns.end(), trains.begin(),
> boost::mem_fun(&Neuron::get_ptr_train));

I can see three problems with the code.

First, boost::mem_fun from <boost/functional.hpp> does not take a
boost::shared_ptr. You may use the mem_fun from <boost/mem_fun.hpp> that
does (it's part of the Bind library, currently under review.)

Second, your get_ptr_train() function returns a raw pointer to, I assume,
Neuron::st. You can't assign this to a boost::shared_ptr<const
Train<float_t> >, as you try to do with the transform() call above; even if
you could do the assignment, the shared_ptr would try to delete the pointer
in its destructor. Your 'trains' array should probably look like

std::vector<const Train<float_t> *> trains;

Third, transform'ing into trains.begin() will overwrite the current contents
of trains; I don't know whether this is intentional or you'd need
back_inserter(trains) instead.

Peter Dimov
Multi Media Ltd.

Boost list run by bdawes at, gregod at, cpdaniel at, john at