Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-07-13 12:43:53


Arash Partow wrote:
> Hi all,
>
> I'm having some difficulties getting the following piece of code to
> compile:
>
>
> #include <iostream>
> #include <vector>
>
> #include <boost/utility.hpp>
> #include <boost/bind.hpp>
> #include <boost/tuple/tuple.hpp>
>
>
> class AClass
> {
> public:
> AClass(){}
> void b(int i, int j, int k)
> {
> std:: cout << "a::b() - " << i << "\t" << j << "\t" << k <<
> std::endl; }
> };
>
> template <typename T,int i> class select
> {
> public:
> typedef typename boost::tuples::element<i,T>::type result_type;
>
> template <typename U>
> result_type operator()(const U& u) const

The simpler

   result_type operator()(const T& u) const

ought to work, too.

> {
> return u.get<i>();

You might need u.template get<i>(); here depending on the compiler.

> }
>
> };
>
> int main(int argc, char* argv[])
> {
> typedef boost::tuples::tuple<AClass,int,int,int> param_set;
>
> std::vector<param_set> list;
> list.push_back(param_set(AClass(),1,2,3));
> list.push_back(param_set(AClass(),2,3,4));
> list.push_back(param_set(AClass(),3,4,5));
> list.push_back(param_set(AClass(),4,5,6));
> list.push_back(param_set(AClass(),5,6,7));
>
> std::for_each(
> list.begin(),
> list.end(),
> boost::bind(&AClass::b,
> boost::bind(select<param_set,0>(),_0),

There is no placeholder named _0, they start at _1.

> boost::bind(select<param_set,0>(),_1),
> boost::bind(select<param_set,0>(),_2),
> boost::bind(select<param_set,0>(),_3)));

You don't have enough arguments to use _2 or _3 as for_each only passes one.
Instead of trying to get<0> the first element of the four tuples _0.._3, you
need to get<0..3> the four elements of the one tuple _1.

                               boost::bind(select<param_set,0>(),_1),
                               boost::bind(select<param_set,1>(),_1),
                               boost::bind(select<param_set,2>(),_1),
                               boost::bind(select<param_set,3>(),_1)));


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