
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 { return u.get<i>(); } }; 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), boost::bind(select<param_set,0>(),_1), boost::bind(select<param_set,0>(),_2), boost::bind(select<param_set,0>(),_3))); return true; } Basically the "select class" is a code snippet I found on this list, the compilation errors I believe exist in how I'm calling bind, but I cant tell whats going wrong. Also I'm wondering is there a better way of doing what I'm trying to do, is there already a class or something in boost that will allow one to bind a function and a tuples of parameters. Also I'm wondering if it will be possible to derive AClass from boost::noncopyable and still be able to instantiate class instances as part of method parameters. Any help would be very much appreciated. Arash Partow ________________________________________________________ Be one who knows what they don't know, Instead of being one who knows not what they don't know, Thinking they know everything about all things. http://www.partow.net

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)));
participants (2)
-
Arash Partow
-
Peter Dimov