Using legacy binders and C ++ 0x lambdas

C ++ 0x refused to use old ties, such as bind1st, and bind2ndin favor of the total std::bind. C ++ 0x lambdas are perfectly combined with std::bind, but they do not bind to classical bind1st and bind2nd, because the default lambdas are not nested typedefs, such as argument_type, first_argument_type, second_argument_typeand result_type, so I think that std::functioncan serve as a standard method to bind lambdas old ties, since it provides the necessary typedefs.

However, use is std::functiondifficult to use in this context, as it forces you to specify the type of function when creating 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_fuctionit would be nice to have. Is there such a thing? If not, is there another better way to bind lamd to classic binders?

+3
source share
2 answers

I never tried to do such a thing, and I do not have time to give a complete answer, but I think that something could be done using Boost.FunctionTypes.

Here is a rough, incomplete and untested project 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);
}
+2
source

, bind1st .. , , . , bind1st(f, a) [a](v){ return f(a, v); } .., .

+1

Source: https://habr.com/ru/post/1740191/


All Articles