Sorry I don't know how to express it in English, so I use some codes to show what I am trying to say

template<typename strategyOne, typename strategyTwo, typename strategyThree>
void someCode(strategyOne stOne, strategyTwo stTwo, strategyThree stThree)
{
 //some codes
 stOne(some args);
 //some codes
 stTwo(some args);
 //some codes
 stThree(some args);
}

int main()
{
 //assume that I have A number of strategies for the strategyOne
 //B number for strategyTwo, C number for strategyThree
 //many jobs of initialization, like
 boost::functional<....> f1 = boost::bind(.....);
 //fx belongs to the family of strategyOne,
 //gx belongs to the family of strategyTwo
 //hx belongs to the family of strategyThree
 
//this loop will test different combinations of all of the strategies
//I declared above
 for(....)
 {
  //fx = f0, f1, f2, and so on
  //gx = g0, g1, g2 and so on
  //hx = h0, h1, h2 and so on
  someCode(fx, gx, hx);
 }
}

I am doing some experiment on what kind of strategies could get
the best result under the certain flow
And I want to pick out the results I like most from the combinations and optimize it(maybe at that time I have to change the flow of the function at all)
How to make this kind of program work?
If there are anything I didn't express it precise and clear, please let me know

Thanks a lot

Recommended Answers

All 3 Replies

Well, do you really have that many alternative strategies? For a test program like this, I don't see much alternative to just listing out the combinations and executing them one after the other.

Anyhow, you could just make f, g, and h as static arrays of boost::function<> and loop through all of them (three nested loops). For instance:

int main()
{
 boost::function<....> f[] = { boost::bind(.....),
                               boost::bind(.....),
                               boost::bind(.....) };
                               
 //similarly for g and h.
 
 for(int i = 0; i < sizeof(f); ++i)
   for(int j = 0; j < sizeof(g); ++j) 
     for(int k = 0; k < sizeof(h); ++k)
       someCode(f[i], g[j], h[k]);
}

Sorry, I don't know how to bind several strategies to boost::function
simultaneously.

template<typename T> T foobar( T a, T b ) { return a + b ;}
template<typename T> T foobar( T a, T& b, T c ) { return b = a - b + c; }

void testBind()
{
  int(&int_foobar)(int, int) = foobar;  
  boost::function< int(int) > f1 = boost::bind( int_foobar, _1, 23 );
  std::cout << f1(99) << '\n' ;

  double b = 56.2;
  double(&dbl_foobar)(double, double&, double) = foobar; 
  boost::function<double(double, double)> f2 = boost::bind(dbl_foobar, _1, boost::ref(b), _2 );
  std::cout <<f2( 17.4, 23.9)<< ' ' <<b<< '\n';

  //boost::function<> f9[2] = {f1, f2}; don't know how to bind it
}

Well, do you really have that many alternative strategies?

Not for now, some of them are under developing, and this method can't be
a good solution due to the exponential growing speed of the number of
those combination strategies.But this would be fun and cool if it really
could be done(just my opion).

Thanks a lot

I figure it out by myself

template<typename T> T foobar( T a, T b ) { return a + b ;}
template<typename T> T foobar( T a, T& b, T c ) { return b = a - b + c; }

void testBind()
{
  int(&int_foobar)(int, int) = foobar;  
  boost::function< int(int) > f1 = boost::bind( int_foobar, _1, 23 );
  std::cout << f1(99) << '\n' ;

  double b = 56.2;
  double(&dbl_foobar)(double, double&, double) = foobar; 
  boost::function<double(double, double)> f2 = boost::bind(dbl_foobar, _1, boost::ref(b), _2 );
  std::cout <<f2( 17.4, 23.9)<< ' ' <<b<< '\n';

  //boost::function<> f9[2] = {f1, f2}; can't bind with different arguments list?
boost::function<int(int)> f9[2] = {f1, f1};//this one is ok
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.