Topic: stl:list and boost:function/bind as messagehandler
Author: andrescNO@SPAMweb.de (=?ISO-8859-15?Q?Andr=E9_Schmidt?=)
Date: Mon, 24 Jan 2005 18:18:16 GMT Raw View
Hi newsgroup!
I am in trouble storing boost function-pointers for=20
object-member-functions in a std:list
defining in Input.h
typedef boost::function2< void, INPUT_EVENT, int> PInputHandler;
std::list<PInputHandler> m_listHandler;
void CInput::RegisterTarget( PInputHandler pHandler )
{
m_listHandler.push_back(pHandler);
}
using in an instance of CCtrlButton
g_input.RegisterTarget(boost::bind( &CCtrlButton::HandleInputEvents,=20
this, _1, _2 ) );
All works fine until i want to remove a pointer from the list:
void CInput::ReleaseTarget( PInputHandler pHandler )
{
m_listHandler.remove(pHandler);
}
MSVC6 gives the following Error in the definition of std::list::remove
D:\Programme\Microsoft Visual Studio\VC98\INCLUDE\list(280) : error=20
C2451: Bedingter Ausdruck des Typs 'void' nicht zulaessig
Ausdruck vom Typ void kann nicht in andere Typen konvertiert wer=
den
D:\Programme\Microsoft Visual Studio\VC98\INCLUDE\list(278) :=20
Bei der Kompilierung der Member-Funktion 'void __thiscall=20
std::list<class boost::function2<void,enum INPUT_EVENT,int,int>,class=20
std::allocator<class boost::function2<void,enum INP
UT_EVENT,int,int> > >::remove(const class boost::function2<void,enum=20
INPUT_EVENT,int,int> &)' der Klassenvorlage
excuse me that it is in german language, i try to translate it:
"Conditional expression of type 'void' is not valid. Expression of type=20
void cannot be cast into other types"
here is the definition of std::list::remove
void remove(const _Ty& _V)
{iterator _L =3D end();
for (iterator _F =3D begin(); _F !=3D _L; )
if (*_F =3D=3D _V)
erase(_F++);
else
++_F; }
Something i missed?
I think its the comparison of the to handler-objects that did notwok.
Thanks in advance,
Andr=E9
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: doug.gregor@gmail.com
Date: Mon, 24 Jan 2005 23:20:19 CST Raw View
It is not possible to compare two Boost.Function objects (because it
can't be implemented "well"). However, recent versions of
Boost.Function can be compared against any type that they can store.
For instance:
function<int(int, int)> f;
function<int(int, int)> g;
if (f == g) ; // ERROR! cannot do this
f = std::plus<int>();
assert(f == std::plus<int>()); // OK
This is enough functionality to implement a template variation of your
ReleaseTarget function, like this:
template<typename Handler>
void ReleaseTarget( const Handler& pHandler )
{
// Can't use remove(), unfortunately, because pHandler would get
converted.
std::list<PInputHandler>::iterator i = m_listHandler.begin();
while (i != m_listHandler.end()) {
if (*i == pHandler) i = m_listHandler.erase(i);
else ++i;
}
}
Doug Gregor
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]