问题
Is there a way to overload, say the >>
operator for function composition? The operator should work seamlessly on lambdas as well as std::function
?
Requirements:
- The solution should not include nested
bind
calls, - the left operand can be of a functional type with an arbitrary number of parameters, and
- no more than one function object instance should be created.
Here is a quick and dirty example that illustrates the desired behaviour:
#include <iostream>
#include <functional>
using namespace std;
// An example of a quick and dirty function composition.
// Note that instead of 'std::function' this operator should accept
// any functional/callable type (just like 'bind').
template<typename R1, typename R2, typename... ArgTypes1>
function<R2(ArgTypes1...)> operator >> (
const function<R1(ArgTypes1...)>& f1,
const function<R2(R1)>& f2) {
return [=](ArgTypes1... args){ return f2(f1(args...)); };
}
int main(int argc, char **args) {
auto l1 = [](int i, int j) {return i + j;};
auto l2 = [](int i) {return i * i;};
function<int(int, int)> f1 = l1;
function<int(int)> f2 = l2;
cout << "Function composition: " << (f1 >> f2)(3, 5) << endl;
// The following is desired, but it doesn't compile as it is:
cout << "Function composition: " << (l1 >> l2)(3, 5) << endl;
return 0;
}
回答1:
(l1 >> l2)
can never work.
They are function objects made by the compiler and don't include that operator, so unless you plan on modifying the compiler to be non-conforming that's how it's always going to be. :)
You can, however, introduce a "keyword" (utility class) which is arguably a good thing, but it's hefty:
// https://ideone.com/MS2E3
#include <iostream>
#include <functional>
namespace detail
{
template <typename R, typename... Args>
class composed_function;
// utility stuff
template <typename... Args>
struct variadic_typedef;
template <typename Func>
struct callable_type_info :
callable_type_info<decltype(&Func::operator())>
{};
template <typename Func>
struct callable_type_info<Func*> :
callable_type_info<Func>
{};
template <typename DeducedR, typename... DeducedArgs>
struct callable_type_info<DeducedR(DeducedArgs...)>
{
typedef DeducedR return_type;
typedef variadic_typedef<DeducedArgs...> args_type;
};
template <typename O, typename DeducedR, typename... DeducedArgs>
struct callable_type_info<DeducedR (O::*)(DeducedArgs...) const>
{
typedef DeducedR return_type;
typedef variadic_typedef<DeducedArgs...> args_type;
};
template <typename DeducedR, typename... DeducedArgs>
struct callable_type_info<std::function<DeducedR(DeducedArgs...)>>
{
typedef DeducedR return_type;
typedef variadic_typedef<DeducedArgs...> args_type;
};
template <typename Func>
struct return_type
{
typedef typename callable_type_info<Func>::return_type type;
};
template <typename Func>
struct args_type
{
typedef typename callable_type_info<Func>::args_type type;
};
template <typename FuncR, typename... FuncArgs>
struct composed_function_type
{
typedef composed_function<FuncR, FuncArgs...> type;
};
template <typename FuncR, typename... FuncArgs>
struct composed_function_type<FuncR, variadic_typedef<FuncArgs...>> :
composed_function_type<FuncR, FuncArgs...>
{};
template <typename R, typename... Args>
class composed_function
{
public:
composed_function(std::function<R(Args...)> func) :
mFunction(std::move(func))
{}
template <typename... CallArgs>
R operator()(CallArgs&&... args)
{
return mFunction(std::forward<CallArgs>(args)...);
}
template <typename Func>
typename composed_function_type<
typename return_type<Func>::type, Args...>::type
operator>>(Func func) /* && */ // rvalues only (unsupported for now)
{
std::function<R(Args...)> thisFunc = std::move(mFunction);
return typename composed_function_type<
typename return_type<Func>::type, Args...>::type(
[=](Args... args)
{
return func(thisFunc(args...));
});
}
private:
std::function<R(Args...)> mFunction;
};
}
template <typename Func>
typename detail::composed_function_type<
typename detail::return_type<Func>::type,
typename detail::args_type<Func>::type>::type
compose(Func func)
{
return typename detail::composed_function_type<
typename detail::return_type<Func>::type,
typename detail::args_type<Func>::type>::type(func);
}
int main()
{
using namespace std;
auto l1 = [](int i, int j) {return i + j;};
auto l2 = [](int i) {return i * i;};
std:function<int(int, int)> f1 = l1;
function<int(int)> f2 = l2;
cout << "Function composition: " << (compose(f1) >> f2)(3, 5) << endl;
cout << "Function composition: " << (compose(l1) >> l2)(3, 5) << endl;
cout << "Function composition: " << (compose(f1) >> l2)(3, 5) << endl;
cout << "Function composition: " << (compose(l1) >> f2)(3, 5) << endl;
return 0;
That's a quite a bit of code! Unfortunately I don't see how it can be reduced any.
You can go another route and just make it so to use lambdas in your scheme, you just have to explicitly make them std::function<>
s, but it's less uniform. Some of the machinery above could be used to make some sort of to_function()
function for making lambda functions into std::function<>
s.
回答2:
This code should do the job. I've simplified it so these only work on functions with one argument, but you should be able to extend it to take functions of more than one argument by some variadic template magic. Also you'd may want to restrict the << operator appropriately.
#include <iostream>
template <class LAMBDA, class ARG>
auto apply(LAMBDA&& l, ARG&& arg) -> decltype(l(arg))
{
return l(arg);
}
template <class LAMBDA1, class LAMBDA2>
class compose_class
{
public:
LAMBDA1 l1;
LAMBDA2 l2;
template <class ARG>
auto operator()(ARG&& arg) ->
decltype(apply(l2, apply(l1, std::forward<ARG>(arg))))
{ return apply(l2, apply(l1, std::forward<ARG>(arg))); }
compose_class(LAMBDA1&& l1, LAMBDA2&& l2)
: l1(std::forward<LAMBDA1>(l1)), l2(std::forward<LAMBDA2>(l2)) {}
};
template <class LAMBDA1, class LAMBDA2>
auto operator>>(LAMBDA1&& l1, LAMBDA2&& l2) -> compose_class<LAMBDA1, LAMBDA2>
{
return compose_class<LAMBDA1, LAMBDA2>
(std::forward<LAMBDA1>(l1), std::forward<LAMBDA2>(l2));
}
int main()
{
auto l1 = [](int i) { return i + 2; };
auto l2 = [](int i) { return i * i; };
std::cout << (l1 >> l2)(3) << std::endl;
}
(p.s. You probably don't need the indirection of "apply", just had some trouble compiling without it)
回答3:
Although as GMan noticed l1 >> l2
will never work, something similar does work, and even produces produces somewhat pretty results. Add this to his code:
class compose_syntax_helper_middle
{
} o;
template <typename Func>
typename detail::composed_function_type<
typename detail::return_type<Func>::type,
typename detail::args_type<Func>::type>::type
operator<< (Func func, compose_syntax_helper_middle)
{
return typename detail::composed_function_type<
typename detail::return_type<Func>::type,
typename detail::args_type<Func>::type>::type(func);
}
Now this syntax works:
(func1 <<o>> func2) (arg1, arg2)
The << >>
operators are intended to be interpreted as a kind of quotes, and o
is a kind of function composition circle thingy...
回答4:
How about this one?
#include <cstdio>
#include <functional>
template <typename F, typename F_ret, typename... F_args,
typename G, typename G_ret, typename... G_args>
std::function<G_ret (F_args...)>
composer(F f, F_ret (F::*)(F_args...) const ,
G g, G_ret (G::*)(G_args...) const)
{
// Cannot create and return a lambda. So using std::function as a lambda holder.
std::function<G_ret (F_args...)> holder;
holder = [f, g](F_args... args) { return g(f(args...)); };
return holder;
}
template<typename F, typename G>
auto operator >> (F f, G g)
-> decltype(composer(f, &F::operator(), g, &G::operator()))
{
return composer(f, &F::operator(), g, &G::operator());
}
int main(void)
{
auto l1 = [](int i , int j) { return i + j; };
auto l2 = [](int a) { return a*a; };
printf("%d\n", (l1 >> l2 >> l2)(2, 3)); // prints 625
return 0;
}
Edit:
Here is some enhanced code with support for free function pointers and member function pointers. I've some test code too. Beware of the number of virtual function calls taking place when you execute such deeply composed std::function objects. I think there is one virtual function call per operator() of a std::function object. Memory allocation and deallocation is another thing you have to keep in mind.
#include <cstdio>
#include <functional>
template <typename F, typename F_ret, typename... F_args,
typename G, typename G_ret, typename... G_args>
std::function<G_ret (F_args...)>
composer(F f, F_ret (F::*)(F_args...) const ,
G g, G_ret (G::*)(G_args...) const)
{
// Cannot create and return a lambda. So using std::function as a lambda holder.
std::function<G_ret (F_args...)> holder;
holder = [f, g](F_args... args) { return g(f(args...)); };
return holder;
}
template<typename F_ret, typename... F_args>
std::function<F_ret (F_args...)>
make_function (F_ret (*f)(F_args...))
{
// Not sure why this helper isn't available out of the box.
return f;
}
template<typename F, typename F_ret, typename... F_args>
std::function<F_ret (F_args...)>
make_function (F_ret (F::*func)(F_args...), F & obj)
{
// Composing a member function pointer and an object.
// This one is probably doable without using a lambda.
std::function<F_ret (F_args...)> holder;
holder = [func, &obj](F_args... args) { return (obj.*func)(args...); };
return holder;
}
template<typename F, typename F_ret, typename... F_args>
std::function<F_ret (F_args...)>
make_function (F_ret (F::*func)(F_args...) const, F const & obj)
{
// Composing a const member function pointer and a const object.
// This one is probably doable without using a lambda.
std::function<F_ret (F_args...)> holder;
holder = [func, &obj](F_args... args) { return (obj.*func)(args...); };
return holder;
}
template<typename F, typename G>
auto operator >> (F f, G g)
-> decltype(composer(f, &F::operator(), g, &G::operator()))
{
return composer(f, &F::operator(), g, &G::operator());
}
// This one allows a free function pointer to be the second parameter
template<typename F, typename G_ret, typename... G_args>
auto operator >> (F f, G_ret (*g)(G_args...))
-> decltype(f >> make_function(g))
{
return f >> make_function(g);
}
// This one allows a free function pointer to be the first parameter
template<typename F, typename G_ret, typename... G_args>
auto operator >> (G_ret (*g)(G_args...), F f)
-> decltype(make_function(g) >> f)
{
return make_function(g) >> f;
}
// Not possible to have function pointers on both sides of the binary operator >>
int increment(int i) {
return i+1;
}
int sum(int i, int j) {
return i+j;
}
struct math {
int increment (int i) {
return i+1;
}
int sum (int i, int j) const {
return i+j;
}
};
int main(void)
{
auto l1 = [](int i , int j) { return i + j; };
auto l2 = [](int a) { return a*a; };
auto l3 = l1 >> l2 >> l2 >> increment; // does 11 allocs on Linux
printf("%d\n", l3(2, 3)); // prints 626
printf("%d\n", (sum >> l2)(3, 3)); // prints 36
math m;
printf("%d\n",
(make_function(&math::sum, m) >> make_function(&math::increment, m))(2, 3)); // prints 6
return 0;
}
回答5:
If you want to accept any kind of function object, your operator >>
should accept most general types.
template<typename F, typename G>
class compose {...}
template<typename f, typename g>
compose <F, G> operator >> (F f, G g)
{ return compose<F, G>(f, g); }
It should be easy enough to figure out what compose::operator()
should do. Hint: it must be a template.
UPD: apparently, C++ is not happy when applying operator >>
to builtin function type. This is not a big problem, the syntax can be modified slightly. The bigger problem is that g++-4.6.0 (the only compiler I currently have that supports parts of c++0x) doesn't seem to cope with the language features needed to implement this. It gives me some very strange errors, and sometimes internal compiler errors. I'll try to upgrade to 4.6.1 and see what happens.
来源:https://stackoverflow.com/questions/6856331/how-to-overload-an-operator-for-composition-of-functionals-in-c0x