Archived

This topic is now archived and is closed to further replies.

Shannon Barber

simple(?) STL adaptor question

Recommended Posts

Shannon Barber    1681
I''m trying to do something that I think ought be very simple... but I just don''t now enough about adaptors to understand what exactly I need to do. How do I use the STL adaptors to call a method on a range of iterators _and pass that method a parameter? This is my conventional loop...
  
vector<udpPacket> m_RecvPacketPool(32);
m_RecvPacketPool.resize(32);

for(int i=0; i<100; ++i)
	m_RecvPacketPool[i].Alloc(2048);
  
...that I''d like to replace with an STL for_each function These are my feable attemps:
  
for_each(m_RecvPacketPool.begin(), m_RecvPacketPool.end(), mem_fun1_ref(udpPacket::Alloc, 2048));

for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), bind1st(mem_fun1_ref(udpPacket::Alloc), 2048));

for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), bind2nd(mem_fun1_ref(udpPacket::Alloc), 2048));

  
neither of which work (MSVC6 with the built-in STL) Here''s the diagnostic mess:
quote:
c:\program files\microsoft\visual studio 6\vc98\include\algorithm(37) : error C2664: ''()'' : cannot convert parameter 1 from ''struct I3D::Socket::udpPacket'' to ''const unsigned int &'' Reason: cannot convert from ''struct I3D::Socket::udpPacket'' to ''const unsigned int'' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called E:\Projects\MSVC\SocketTest\SocketTestDlg.cpp(77) : see reference to function template instantiation ''class std::binder1st > __cdecl std::for_each(struct I3D::Socket:: udpPacket *,struct I3D::Socket::udpPacket *,class std::binder1st >)'' being compiled c:\program files\microsoft\visual studio 6\vc98\include\algorithm(37) : error C2064: term does not evaluate to a function E:\Projects\MSVC\SocketTest\SocketTestDlg.cpp(77) : see reference to function template instantiation ''class std::binder1st > __cdecl std::for_each(struct I3D::Socket:: udpPacket *,struct I3D::Socket::udpPacket *,class std::binder1st >)'' being compiled
Here''s the signiture of the Alloc & Test methods:
  
int Alloc(u32 cBytes);
int Test();
  
This does work...
  
for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), mem_fun_ref(udpPacket::Test));
  
...it doesn''t do anything useful, but show that I figured out how to call a method with no parameters (And before some smart-ass drops the "search google" thing on me, there''s only two hits - one''s a post-script file and the other is in Japanese!) Magmai Kai Holmlor "Oh, like you''ve never written buggy code" - Lee "What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Gorg    248
I never got the binders to work under MSVC. What you are trying to do is the reason why I dropped it.

When I saw you problem, I actually tried it again. I still could not make it work!

On more compliant compilers, you simply need to this :


for_each(vRecvPacketPool.begin(), vRecvPacketPool.end(), bind2nd(mem_fun_ref(&udpPacket::Alloc), 2048));


No need to worry about mem_funX_ref, those are simply supporting classes.

I am sorry I was not of any help.

Edited by - Gorg on December 18, 2001 6:09:19 PM

Share this post


Link to post
Share on other sites
Kylotan    9988
Binders work under MSVC, but only for the simple stuff apparently. I think that when you start throwing the odd mem_fun in there too, it all dies horribly. I can confirm that the example from the Josuttis STL book which shows exactly how to do this sort of thing (bottom of page 307) does not compile under MSVC6. So, take heart that it''s a compiler issue and not your coding. (And be annoyed cos MSVC doesn''t get much better with templates in the next version, either.)

Share this post


Link to post
Share on other sites
Void    126
I would suggest getting a more compliant compiler to find out whose fault is is when u working with STL.

The comeau compiler has the best compliance I know. (www.comeaucomputing.com)

Other free compilers are the gcc port (www.mingw.org) or the borland line compiler

Share this post


Link to post
Share on other sites
Shannon Barber    1681
This is for an exist program built using MFC and is a delivered customer program... so I kinda want to stick to the same compiler and IDE. I did try out the STLport, but it fails as expected since the root problem is a compiler limitation.

  
#define FOR_EACH(begin, end, pred, iter)\
{\
iter it = begin;\
iter itEnd = end;\
for(;it!=itEnd; ++it)\
{\
it pred;\
}\
}

FOR_EACH(m_RecvPacketPool.begin(), m_RecvPacketPool.end(), ->Alloc(2048), tyPacketPool::iterator);

Crude but effective (pObject ->Method(); actually compiles - I tried it half figuring it wouldn''t)

Anyone see a way to determine the iterator type automatically?


...
gcc is a GNU program correct? Do you have to give away any/every program you write with it?

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Kylotan    9988
GCC is the GNU compiler, yeah, but it doesn''t impose any restrictions on what you do with the output. The GNU license applies to what you do with the source code and derivatives of the source, not the output produced by the program.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites