Archived

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

aboeing

for_each - VC6

Recommended Posts

Uh, this is probably a stupid question, but how do you use for_each with VC6? the code im trying is taken straight from SGI:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

struct B {
  virtual void print() = 0;
};
struct D1 : public B {
  void print() { cout << "I''m a D1" << endl; }
};
struct D2 : public B {
  void print() { cout << "I''m a D2" << endl; }
};
int main()
{
  vector<D1> V;
  V.push_back(D1());
  V.push_back(D1());
  for_each(V.begin(), V.end(), mem_fun_ref(&B::print));
}
It compiles and works fine with gcc, I even tryd STLport, same problem:
c:\program files\microsoft visual studio\vc98\include\functional(263) : error C2562: ''()'' : ''void'' function returning a value
        c:\program files\microsoft visual studio\vc98\include\functional(262) : see declaration of ''()''
        c:\program files\microsoft visual studio\vc98\include\functional(263) : while compiling class-template member function ''void __thiscall std::mem_fun_ref_t::operator ()(struct B &) const''
Error executing cl.exe.
 
Thanks.

Share this post


Link to post
Share on other sites
Funnily enough, boost::bind works:

for_each(V.begin(), V.end(), boost::bind(&B:rint,_1));

But im not sure how to use bind for calling a member function with a parameter, ie: (example stolen from some gamedev thread)

#include <vector>
#include <algorithm>
class C
{
public:
void draw()
{
printf("draw");
}
void drawi(int i) {
printf("d%d",i);
}
};
typedef C* c_ptr;
int main()
{
std::vector<c_ptr> v;
for(int i=0; i<10; ++i)
v.push_back(c_ptr(new C));
//this works:

for_each(v.begin(), v.end(), boost::bind(&C::draw,_1));//this is wrong:

int param=9;
for_each(v.begin(), v.end(), boost::bind(&C::drawi,_1)(param));
}

can anyone tell me the correct usage for bind in this instance? thanks.

Share this post


Link to post
Share on other sites
try something like this:


#include <iostream>
#include <vector>
#include <algorithm>

#include <boost/bind.hpp>

using namespace std;

class test
{
public:
void draw()
{
cout << "draw()" << endl;
}
void draw(int i)
{
cout << "draw(" << i << ")" << endl;
}
};

int main(int argc,char* argv[])
{
typedef test* t_ptr;
vector<t_ptr> v;
for(int i = 0; i < 10; ++i)
v.push_back(t_ptr(new test));

for_each(v.begin(),v.end(),boost::bind(&test::draw,_1));
int n = 9;
for_each(v.begin(),v.end(),boost::bind(&test::draw,_1,n));
return 0;
}

Share this post


Link to post
Share on other sites
Well, sure you could do something like that.

if you do a:

functor = boost::bind(&a:rint,_1,_2)

You''d call it like this:

a* _a = new a;

functor(a,10);

However, if you did this:

functor = boost::bind(&a:rint,_1,10);

you''d call it like this:

functor(a);

and the functor will automagically supply the 10

Share this post


Link to post
Share on other sites
Partial evaluation?! Way cool!

I always figured they''d be able to support it but didn''t know they already did. Say I have a function of n arguments and I supply two of them. Does the binder generate a function that needs n-2 arguments?

Share this post


Link to post
Share on other sites
Kool thanks daerid, that helped clear things up a bit
I would still be interested in finding out why the original code didn''t compile with VC6.. is it a compiler problem then?

Also, is there any good documentation on boost? I find the documentation that it comes with leaves a bit to be desired.

Share this post


Link to post
Share on other sites
The only docs that I know of are at http://www.boost.org

I had to struggle along with them, as well as just test out all the different libraries a bunch of times before I wrapped my wits around ''em.

Share this post


Link to post
Share on other sites