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 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
using namespace std;
using namespace boost;
void listenerA() {}
void listenerB() {}
int main()
{
typedef function 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 ==(const
boost::function0 &,const boost::function0 &)'
[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 ==(Functor,const
boost::function_base &)' [found using argument-dependent lookup]
with
[
Signature=void (void),
Functor=boost::function
]
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,
Functor=boost::function
]
or 'built-in C++ operator==(void (__thiscall
boost::function0::dummy::* )(void), void (__thiscall
boost::function0::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,_Ty>(_InIt,_InIt,const _Ty
&)' being compiled
with
[
_InIt=std::_Vector_iterator,
_Ty=Listener,
_Alloc=std::allocator<Listener>
]
d:\development\misc\functionvector\functionvector.cpp(35) : see
reference to function template instantiation '_InIt
std::find,boost::function<Signature>>(_InIt,_InIt,const
_Ty &)' being compiled
with
[
_InIt=std::_Vector_iterator,
_Ty=Listener,
_Alloc=std::allocator<Listener>,
Signature=void (void)
]
Best regards,
Christian