I'm having some problems with bind and wonder if someone might help. The attached file compiles and runs fine when compiled with boost-1.30.2 (g++ 3.2 on a PC and g++ 3.3 on a Dec Alpha, both running linux). However, compilation fails if I uncomment the overloaded 'node' member function below. struct FEM { typedef std::vector<Node>::size_type size_type; FEM(std::vector<Node> const & nodes) : nodes_(nodes) {} size_type nnodes() const { return nodes_.size(); } Node & node(size_type i) { return nodes_[i]; } // Node const & node(size_type i) const { return nodes_[i]; } private: std::vector<Node> nodes_; }; boost/boost-1.30.2/boost/function/function_template.hpp:117: could not convert `f->boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = size_t, R = const Node&, F = boost::_mfi::cmf1<const Node&, FEM, unsigned int>, L = boost::_bi::list2<boost::reference_wrapper<FEM>, boost::arg<1> >]((&a0))' to `Node&' Is there a fix for this or have I hit a brick wall? Second question. I'd like to combine these two boost::functions into a single boost::function2<void, size_t, double> all_in_one = ...; all_in_one(i, val); but I'm getting horribly lost trying to determine the '...'. Is it possible? Any clues? Kind regards, Angus ps. What is the prefered way of posting code? Inline or as an attachment? A std::vector<Node> nodes(10); FEM fem(nodes); size_t i = 5; double const val = 14; boost::function1<Node &, size_t> get_node = boost::bind(&FEM::node, boost::ref(fem), _1); boost::function1<void, double> set_value = boost::bind(&Node::set_conc, boost::ref(get_node(i)), _1); set_value(val);
Angus Leeming wrote: Apologies for replying to my own mail, but I've made some progress. The attached code allows me to do all I would like to do, although it is more ugly than I would like. Specifically, the compiler errors caused by the existence of two node functions: class FEM { Node & node(size_type i); Node const & node(size-type i) const; }; Can be overcome if I wrap an FEM variable up inside a struct: class nodeGetter { FEM & fem_; public: nodeGetter(FEM & fem) : fem_(fem) {} Node & node operator()(size_type i) { return fem_.node(i); } }; It works, but it's nasty :-( I'd welcome any and all insights and would just love to get rid of this nastiness. Angus
On Tuesday 04 November 2003 08:47 pm, Angus Leeming wrote:
I'm having some problems with bind and wonder if someone might help. [snip code] boost/boost-1.30.2/boost/function/function_template.hpp:117: could not convert `f->boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = size_t, R = const Node&, F = boost::_mfi::cmf1<const Node&, FEM, unsigned int>, L = boost::_bi::list2<boost::reference_wrapper<FEM>, boost::arg<1> >]((&a0))' to `Node&'
How odd. I would have expected it to be ambiguous at the call to bind, but it takes the const version over the non-const version.
Is there a fix for this or have I hit a brick wall?
Well, you're stuck casting the member function pointer to the right type: boost::function1<Node &, size_t> get_node = boost::bind((Node& (FEM::*)(FEM::size_type))&FEM::node, boost::ref(fem), _1);
Second question. I'd like to combine these two boost::functions into a single boost::function2<void, size_t, double> all_in_one = ...; all_in_one(i, val); but I'm getting horribly lost trying to determine the '...'. Is it possible? Any clues?
Ah, the beauty of nesting bind() expressions. Start with the outer call (set_conc), and work your way inward by writing the parameters to it. Here's the final solution (with ugly casting hack): boost::function2<void, size_t, double> all_in_one = boost::bind(&Node::set_conc, boost::bind((Node& (FEM::*)(FEM::size_type))&FEM::node, boost::ref(fem), _1), _2); Doug
participants (2)
-
Angus Leeming
-
Douglas Gregor