is it possible to bind 2 default parameters using function object(STL)?

T

thinktwice

here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)
 
N

n2xssvv g02gfr12930

thinktwice said:
here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)
What you probably need is a Functor object. Something like the code below:

class FunctorExample
{
private:
int arg2;
Obj &Data;
public:
FunctorExample(Obj &Ref, int Val) : Data(Ref),arg2(val) {}
void operator() (int arg1)
{
Data.func(arg1,arg2);
}
};

{
// Some sample code
Obj ExampleObj;
FunctorExample FnctObj(ExampleObj,6);
std::for_each(container.begin(),container.end(),&FnctObj);
}

With a more accurate description/example of your requirements
this could probably be modified to suit.

JB
 
A

Alex Beluga

thinktwice said:
here is what i what to do;
...horrible for loop code eaten out
i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)

Hello.
First, use Boost, Boost is your friend, Boost won't harm you :) More
specifically, I'd recommend you to use Boost.Bind for simplicity sake.
Second, you should've probably use mem_fn here as you're referring to
the obj's class namespace.
Here what kind of pseudocode you should be left over with:
std::for_each(container.begin(),container.end(),
boost::bind<ReturnType>(mem_fn(&myclass::func,arg1,arg2)));
 
V

Victor Bazarov

thinktwice said:
here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?

It's possible if you write your own functor. The standard means only
exist for no- and one-argument member functions.

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;

struct foo {
void bar(int one, double two) {
cout << this << " : " << one << " and " << two << endl;
}
};

template<class T, class A1, class A2> struct mem_fun_2_t {
void (T::*memptr)(A1, A2);
A1 arg1;
A2 arg2;
mem_fun_2_t(void (T::*mp)(A1, A2), A1 a1, A2 a2)
: memptr(mp), arg1(a1), arg2(a2) {}
void operator()(T& t) { return (t.*memptr)(arg1, arg2); }
};

template<class T, class A1, class A2>
mem_fun_2_t<T,A1,A2> mem_fun_2(void (T::*mp)(A1, A2), A1 a1, A2 a2)
{
return mem_fun_2_t<T,A1,A2>(mp, a1, a2);
}

int main()
{
vector<foo> v(10);
for_each(v.begin(), v.end(), mem_fun_2(&foo::bar, 42, 3.14159));
}
 
J

Jeff Flinn

Alex said:
Hello.
First, use Boost, Boost is your friend, Boost won't harm you :) More
specifically, I'd recommend you to use Boost.Bind for simplicity sake.
Second, you should've probably use mem_fn here as you're referring to
the obj's class namespace.
Here what kind of pseudocode you should be left over with:
std::for_each(container.begin(),container.end(),
boost::bind<ReturnType>(mem_fn(&myclass::func,arg1,arg2)));

Not quite.

boost::bind( &myclass::func, _1, arg1, arg2 ) );

There are very few cases where ReturnType needs to be explicitly specified,
mem_fn is not necessary, and you forgot that member functions take an
implicit this pointer.

Jeff Flinn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top