C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

stl:list and boost:function/bind as messagehandler

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
André Schmidt
Guest





PostPosted: Mon Jan 24, 2005 6:18 pm    Post subject: stl:list and boost:function/bind as messagehandler Reply with quote



Hi newsgroup!

I am in trouble storing boost function-pointers for
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,
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:ProgrammeMicrosoft Visual StudioVC98INCLUDElist(280) : error
C2451: Bedingter Ausdruck des Typs 'void' nicht zulaessig
Ausdruck vom Typ void kann nicht in andere Typen konvertiert werden
D:ProgrammeMicrosoft Visual StudioVC98INCLUDElist(278) :
Bei der Kompilierung der Member-Funktion 'void __thiscall
std::list<class boost::function2 std::allocator<class boost::function2 UT_EVENT,int,int> > >::remove(const class boost::function2<void,enum
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
void cannot be cast into other types"



here is the definition of std::list::remove

void remove(const _Ty& _V)
{iterator _L = end();
for (iterator _F = begin(); _F != _L; )
if (*_F == _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é

---
[ 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 ]

Back to top
doug.gregor@gmail.com
Guest





PostPosted: Tue Jan 25, 2005 5:20 am    Post subject: Re: stl:list and boost:function/bind as messagehandler Reply with quote



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 ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.