Boost logo

Boost Users :

From: Christian Larsen (contact_at_[hidden])
Date: 2008-08-18 11:40:15


Hi,

I'm storing function objects in a std::vector, but to remove one again,
I need to find an iterator to the one I want to remove. I do this in an
"unregister" function taking a single boost::function<void ()> as
parameter. Inside the function I would like to use the std::find
algorithm to get an iterator to that function in the vector. I'm getting
compile errors, though; see the example below. How do I fix these?

I'm using MSVC++ 8 (SP1), and Boost 1.33.1 (can not upgrade).

Example, where in my "unregister" function I use find as in the last
part of this example (i.e. I have a Listener and not a plain function
pointer). This does not compile:

     #include <algorithm>
     #include <vector>
     #include <boost/function.hpp>

     using namespace std;
     using namespace boost;

     void listenerA() {}
     void listenerB() {}

     int main()
     {
         typedef function<void ()> Listener;
         typedef vector<Listener> ListenerVector;

         ListenerVector listeners;

         listeners.push_back(&listenerA);
         listeners.push_back(&listenerB);

         ListenerVector::iterator iter;

         // This compiles and works
         iter = find(
             listeners.begin(), listeners.end(),
             &listenerB);

         assert(iter != listeners.end());

         // This gives:
         // error C2666: 'boost::operator ==' : 4 overloads have
         // similar conversions
         iter = find(
             listeners.begin(), listeners.end(),
             Listener(&listenerB));

         assert(iter != listeners.end());

         return 0;
     }

Compiler errors:

c:\program files (x86)\microsoft visual studio
8\vc\include\algorithm(40) : error C2666: 'boost::operator ==' : 4
overloads have similar conversions
 
d:\development\boost_1_33_1\boost\function\function_template.hpp(583):
could be 'void boost::operator ==<R,Allocator>(const
boost::function0<R,Allocator> &,const boost::function0<R,Allocator> &)'
[found using argument-dependent lookup]
         with
         [
             R=void,
             Allocator=std::allocator<void>
         ]
 
d:\development\boost_1_33_1\boost\function\function_base.hpp(609): or
'bool boost::operator ==<boost::function<Signature>>(Functor,const
boost::function_base &)' [found using argument-dependent lookup]
         with
         [
             Signature=void (void),
             Functor=boost::function<void (void)>
         ]
 
d:\development\boost_1_33_1\boost\function\function_base.hpp(600): or
'bool boost::operator ==<_Ty>(const boost::function_base &,Functor)'
[found using argument-dependent lookup]
         with
         [
             _Ty=boost::function<void (void)>,
             Functor=boost::function<void (void)>
         ]
         or 'built-in C++ operator==(void (__thiscall
boost::function0<R,Allocator>::dummy::* )(void), void (__thiscall
boost::function0<R,Allocator>::dummy::* )(void))'
         with
         [
             R=void,
             Allocator=std::allocator<void>
         ]
         while trying to match the argument list
'(boost::function<Signature>, const boost::function<Signature>)'
         with
         [
             Signature=void (void)
         ]
         c:\program files (x86)\microsoft visual studio
8\vc\include\algorithm(74) : see reference to function template
instantiation '_InIt
std::_Find<std::_Vector_iterator<_Ty,_Alloc>,_Ty>(_InIt,_InIt,const _Ty
&)' being compiled
         with
         [
             _InIt=std::_Vector_iterator<Listener,std::allocator<Listener>>,
             _Ty=Listener,
             _Alloc=std::allocator<Listener>
         ]
         d:\development\misc\functionvector\functionvector.cpp(35) : see
reference to function template instantiation '_InIt
std::find<std::_Vector_iterator<_Ty,_Alloc>,boost::function<Signature>>(_InIt,_InIt,const
_Ty &)' being compiled
         with
         [
             _InIt=std::_Vector_iterator<Listener,std::allocator<Listener>>,
             _Ty=Listener,
             _Alloc=std::allocator<Listener>,
             Signature=void (void)
         ]

Best regards,
Christian


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