|
Boost : |
From: Darren Cook (darren_at_[hidden])
Date: 2004-05-13 04:31:56
I've a set of trees (e.g. a set of identically structured XML documents) and
I want to be able to return a reference to a certain position in the tree.
My test tree node is [1] below, with a test tree [2].
I think I can solve this with boost::function and boost::lambda. E.g. this
works:
boost::function1<int,Node&> best2;
best2=bind(&Node::value2,_1);
std::cout<<"best2(root)="<<best2(root)<<"\n";
std::cout<<"best2(child[0])="<<best2(root.children[0])<<"\n";
I also need to be able to treat "number of children" the same way I can
treat value1 or value2. I've now worked this out as:
best2=bind(&std::vector<Node>::size,bind(&Node::children,_1));
Going strong, but now I want to refer to the "value1 of the first child".
Following the patterns of the previous ones I thought it would be:
best2=bind(&Node::value2,
bind( &std::vector<Node>::at, //Ask for an element
bind(&Node::children,_1), //Get the vector object
0 ) //Ask for element zero
);
That fails with a short error message [3], which is so rare when dealing
with BLL that I thought I might be able to understand it, but no, I couldn't
work out which bit it didn't like.
Can anyone help? Also, is there any limit to how deep I can nest these
binds? I think I'm going to need 3 more levels, but it would be useful to
know if it is unlimited.
Darren
[1]:
class Node{
public:
std::vector<Node> children;
int value1,value2;
public:
Node(int v1,int v2):value1(v1),value2(v2){}
friend std::ostream& operator<<(std::ostream& s,const Node& obj){
s<<"value1="<<obj.value1<<",value2="<<obj.value2<<",children.size="<<obj.children.size()<<"\n";
return s;
}
};
[2] Example usage:
Node root(1,111);
root.children.push_back(Node(2,222));
std::cout<<root<<root.children[0];
[3]:
function.cpp: In function `int main(int, char**)':
function.cpp:69: no matching function for call to `bind(<unknown type>, const
boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::action<2,
boost::lambda::function_action<2, boost::lambda::detail::unspecified> >,
boost::tuples::tuple<std::vector<Node, std::allocator<Node> > Node::*, const
boost::lambda::lambda_functor<boost::lambda::placeholder<1> >,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type,
boost::tuples::null_type, boost::tuples::null_type> > >, int)'
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk