On Tue, Oct 18, 2011 at 3:54 PM, Vicente Botet <vicente.botet@wanadoo.fr> wrote:

How is make_generator different from bind?
Why

function<T()> g = bind(f,v);

Doesn't works for you?


Hi Vicente

Thanks for answering. My initial response was "Doh, I've asked a really stupid question here.", which may still
be true! But walk a little with me on this... 

In this code sequence

#include <vector>
#include <iostream>
#include "boost/assign/std/vector.hpp"
#include "boost/function.hpp"
#include "boost/bind.hpp"

using boost::function;
using boost::bind;

void do_it( function<void()> f ) { f( ); }

void print( int i ) { std::cout << i << std::endl; }

template <typename T, typename T1> function<T( )> make_generator( function<T(T1)> f, T1 t1 ) { return bind( f, t1 ); }
template <typename T, typename T1, typename T2> function<T( )> make_generator( function<T(T1, T2)> f, T1 t1, T2 t2 ) { return bind( f, t1, t2 ); }
template <typename T, typename T1, typename T2, typename T3> function<T( )> make_generator( function<T(T1, T2, T3)> f, T1 t1, T2 t2, T3 t3 ) { return bind( f, t1, t2, t3 ); }
// ...etc...

int main( )
{
    using namespace boost::assign;

    std::vector<int> v;
    v += 1,2,3,4,5,6,7;

    for_each( v.begin(), v.end(), bind( do_it, bind( make_generator<void, int>, print, _1 ) ));
}


...how would you write the final for_each using bind directly, given that the other
interfaces are fixed?  I think you'd need to specify the exact types taken
and returned by bind, which are less than convenient to work with.

So I think the answer to 'doesn't bind work for me?', is no, because I'd like
specify the template instantiation types in a more convenient way.

That may all be a load of gibberish - I'm feeling my way a bit here!

Also, if my function signature handling skills were better, it might be better to specify
the template parameter(s) of make_generator as a function signature or MPL type
list.

Thx, Rob.