Boost logo

Boost Users :

From: Nico Galoppo (nico_at_[hidden])
Date: 2006-02-21 20:42:20


David Abrahams wrote:

>>I'm having trouble getting make_filter_iterator and bind to work together nicely:
>>
>>-------------------------------------------------------------------------------
>> boost::function<bool (const node_type&)> is_constraint;
>> is_constraint = boost::bind(&node_type::IsConstraint, _1);
>>
>> boost::make_filter_iterator(is_constraint, ab.begin(), ab.end()); // ERROR:
>>does not compile
>> boost::make_filter_iterator(boost::bind(&node_type::IsConstraint, _1),
>>ab.begin(), ab.end()); // compiles
>>-------------------------------------------------------------------------------
>>
>>Note that ab.begin()/ab.end() return iterators to containers of shared pointers.
>>See below for compiler errors.
>
>
> It's actually not bind, but function. Bind contains magic that treats
> shared_ptr<T> and T& the same way, but for function to take advantage
> of that magic, you need to make sure its argument type is
> shared_ptr<T>, not T&:
>
> boost::function<bool (shared_ptr<node_type const>)> is_constraint
> = boost::bind(&node_type::IsConstraint, _1);
>
> boost::make_filter_iterator(is_constraint, ab.begin(), ab.end());
>

Thanks, that works like a charm. Unfortunately, dereferencing the created
iterator gives more trouble:

   boost::function<bool (shared_ptr<node_type const>)> is_constraint
     = boost::bind(&node_type::IsConstraint, _1);
   typedef boost::filter_iterator<boost::function<bool (shared_ptr<node_type
const>)>, articulated_model_type::node_df_traversal_iterator> joint_node_iterator;

   joint_node_iterator jointnode
     = boost::make_filter_iterator(is_constraint, ab.begin(), ab.end());

   std::cout << jointnode->Model(); ///< this line fails to compile

There seems to be a problem with const-ness (see below).

Thanks again.

--nico

------------------------------------------------------------------------------

d:\dev\boost_1_33_1\boost\iterator\iterator_adaptor.hpp(307) : error C2440:
'return' : cannot convert from 'Hybrid::Graph<node_traits>::const_node_ptr' to
'boost::iterator_facade<Derived,Value,CategoryOrTraversal,Reference,Difference>::reference'
         with
         [
             node_traits=Hybrid::ArticulatedModelNodeTraits<MyTypes>
         ]
         and
         [
             Derived=boost::filter_iterator<boost::function<bool
(boost::shared_ptr<const
Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_type>)>,Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>,
 
Value=boost::iterator_value<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type,
 
CategoryOrTraversal=boost::mpl::identity<boost::forward_traversal_tag>::type,
 
Reference=boost::iterator_reference<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type,
 
Difference=boost::iterator_difference<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type
         ]
         A reference that is not to 'const' cannot be bound to a non-lvalue
         d:\dev\boost_1_33_1\boost\iterator\iterator_adaptor.hpp(307) : while
compiling class-template member function
'boost::iterator_facade<Derived,Value,CategoryOrTraversal,Reference,Difference>::reference
boost::iterator_adaptor<Derived,Base,boost::use_default,Traversal>::dereference(void)
const'
         with
         [
             Derived=boost::filter_iterator<boost::function<bool
(boost::shared_ptr<const
Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_type>)>,Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>,
 
Value=boost::iterator_value<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type,
 
CategoryOrTraversal=boost::mpl::identity<boost::forward_traversal_tag>::type,
 
Reference=boost::iterator_reference<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type,
 
Difference=boost::iterator_difference<Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>::type,
 
Base=Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator,
 
Traversal=boost::mpl::if_<boost::is_convertible<boost::mpl::identity<boost::forward_traversal_tag>::type,boost::random_access_traversal_tag>,boost::bidirectional_traversal_tag,boost::use_default>::type
         ]
         d:\dev\boost_1_33_1\boost\iterator\filter_iterator.hpp(46) : see
reference to class template instantiation
'boost::iterator_adaptor<Derived,Base,Value,Traversal>' being compiled
         with
         [
             Derived=boost::filter_iterator<boost::function<bool
(boost::shared_ptr<const
Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_type>)>,Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator>,
 
Base=Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_
iterator,
             Value=boost::use_default,
 
Traversal=boost::mpl::if_<boost::is_convertible<boost::mpl::identity<boost::forward_traversal_tag>::type,boost::random_access_traversal_tag>,boost::bidirectional_traversal_tag,boost::use_default>::type
         ]
         d:\dev\Hybrid\Tests\Graph\main.cpp(398) : see reference to class
template instantiation 'boost::filter_iterator<Predicate,Iterator>' being compiled
         with
         [
             Predicate=boost::function<bool (boost::shared_ptr<const
Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_type>)>,
 
Iterator=Hybrid::Graph<Hybrid::ArticulatedModelNodeTraits<MyTypes>>::node_df_traversal_iterator
         ]

-- 
nico galoppo von borries     @  address: 105 creel st., chapel hill
comp. graphics phd. student  @           north carolina, 27516 USA
UNC, chapel hill             @  phone: +1 (919) 962-1898 (office)
                              @         +1 (919) 942-7609 (home)
                              @  email: nico at cs dot unc dot edu
                              @  homepage: http://www.cs.unc.edu/~nico
--- debian linux :: vim powered

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