Question

C++0x has deprecated the use of old binders such as bind1st and bind2nd in favor of generic std::bind. C++0x lambdas bind nicely with std::bind but they don't bind with classic bind1st and bind2nd because by default lambdas don't have nested typedefs such as argument_type, first_argument_type, second_argument_type, and result_type. So I thought std::function can serve as a standard way to bind lambdas to the old binders because it exposes the necessary typedefs.

However, using std::function is hard to use in this context because it forces you to spell out the function-type while instantiating it.

auto bound = 
  std::bind1st(std::function<int (int, int)>([](int i, int j){ return i < j; }), 10); // hard to use
auto bound = 
  std::bind1st(std::make_function([](int i, int j){ return i < j; }), 10); // nice to have but does not compile.

I could not find a convenient object generator for std::function. Something like std::make_fuction would be nice to have. Does such a thing exist? If not, is there any other better way of binding lamdas to the classic binders?

Was it helpful?

Solution

Never tried to do such thing, and I don't have the time to provide a full answer, but I guess that something could be done with Boost.FunctionTypes.

Here's a rough, incomplete and untested draft to give you an idea:

template <typename T>
struct AdaptedAsUnary : T
{
    namespace bft = boost::function_types;
    namespace bmpl = boost::mpl;

    typedef typename bft::result_type<T>::type result_type;
    typedef typename bmpl::front<typename bft::parameter_types<T>::type>::type argument_type;

    AdaptedAsUnary(T t) : T(t) {}
};

template <typename T>
AdaptedAsUnary<T>
AdaptAsUnary(T t)
{
    return AdaptedAsUnary<T>(t);
}

OTHER TIPS

I don't know why you still need the bind1st etc. The only thing I could think of is to support old code. Actually, bind1st(f, a) can be replaced by [a](v){ return f(a, v); } etc, and this is a universal solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top