Boost logo

Boost Users :

From: Kevin Wheatley (hxpro_at_[hidden])
Date: 2005-03-17 10:10:57


Peter Dimov wrote:
> Kevin Wheatley wrote:
> > Subsequently, the containers now contain std::pair's to shared_ptrs,
> > so I have even more composition going on, which for a one off
> > 'function' is getting messy, with Lambda it should be much simpler so
> > I'd certainly like to know if I missed something with this.
>
> Extracting a member of a pair is equally painful with Bind and Lambda:
> bind(&Pair::second, _1). Lambda also supports _1->*&Pair::second for
> ordinary pointers, but not for shared_ptr, where you'll need something like
> &*_1->*&Pair::second.

Peter,

Thanks for the reply. It's all starting to look rather like Perl at
this point and as such my inbuild parser is failing to parse what that
does at a glance (can't work out what set of rules I've got to use
:-), so it might be worth avoiding just so that mortals can simply see
what is going on!

I guess if Lambda understood about smart pointers in a similar way to
Bind some of this syntax could be simplfied.

see attatched for what I'd like to do.

Kevin

-- 
| Kevin Wheatley, Cinesite (Europe) Ltd | Nobody thinks this      |
| Senior Technology                     | My employer for certain |
| And Network Systems Architect         | Not even myself         |

#include <vector>
#include <algorithm>
#include <iostream>
#include <cstddef>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/shared_ptr.hpp>

// So don't do this in real code it is not stylish
using boost::shared_ptr;
using namespace boost::lambda;
using namespace std;

int main(int /*argc*/, char** /*argv*/)
{
    const int numberValues = 10;

    vector<int> vi(numberValues);
    for_each(vi.begin(), vi.end(), _1 = 1); // Assign the value 1 to all

    vi[numberValues / 2] = -1;

    vector<int*> vpi(numberValues);
    transform(vi.begin(), vi.end(), vpi.begin(), &_1); // Assign the addresses to the pointers

    for_each(vpi.begin(), vpi.end(),
        cout << _1 << constant(' ')
             << *_1 << constant('\n'));

    vector<shared_ptr<int> > vspi(numberValues);
    for (size_t i = 0; i != vspi.size(); ++i) // Fill with a sequence of new integers [0..numberValues]
        vspi[i].reset(new int(i)); // N.B. Can't use boost::lambda::bind with operator new()
                                               // Ignore the unsigned mismatch warning.

    for_each(vspi.begin(), vspi.end(),
        cout << bind(&shared_ptr<int>::get, _1) << constant(' ')
             << *bind(&shared_ptr<int>::get, _1) << constant('\n'));

    for_each(vspi.begin(), vspi.end(),
        cout << _1 << constant(' ')
             << *_1 << constant('\n')); // Fails here

    return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net