|
Boost Users : |
From: Küppers, Ben (ben.kuppers_at_[hidden])
Date: 2007-06-11 08:17:19
> map<string, shared_ptr<data> > Fred;
>
> RemoveIf(Fred.begin(),
> Fred.end(),
> boost::bind(&map<string, shared_ptr<data>
> >::value_type::second::unique, _1)
> )
James,
remove_if requires an assignment operator on the operated type but the value_type of a map is defined as std::pair< const T1, T2 >. The const on the first type will cause any assignment to fail. In other words, you can't use remove_if on a map.
That said, in order to fix your bind you would need two functors. The type passed to the bind is the value_type of the map which is by definition a pair. You need to first invoke a functor on the pair to extract the second member and then call the member unique on the resulting smart pointer. In order to do so you'll need to write your own extractor, because the STL does not provide a functor to obtain the "second" field from a pair (I haven't found one in boost either).
template< class Pair >
struct second_of : std::unary_function< Pair, typename Pair:second_type > {
typename Pair::second_type opertator()( const Pair& the_pair ) const {
return the_pair.second;
}
};
std::for_each(
Fred.begin(),
Fred.end(),
boost::bind(
boost::shared_ptr< data >::unique,
boost::bind(
second_of< std::map< std::string, boost::shared_ptr< data > >::value_type >(), _1 ) ) );
Note that you can call for_each as it does not require the assignment operator.
Hope this helps,
Ben
This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you.
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