(C++11 newbie here) Trying some composition ideas with C++11 lambdas:
#include <iostream>
#include <functional>
struct A { };
struct B { };
struct C { };
B b4a (A a) { B b; return b; }
C c4b (B b) { C c; return c; }
I now print out the types as follows (the definition of type_name
is a bit off-topic, but I have source of everything in this question building and working online here):
cout << "b4a : " << type_name<decltype(b4a)>() << endl;
cout << "c4b : " << type_name<decltype(c4b)>() << endl;
auto c4a = [=] (A a) { return c4b (b4a (a)); };
cout << "c4a : " << type_name<decltype(c4a)>() << endl;
which produces the following reasonable-looking output:
b4a : B (A)
c4b : C (B)
c4a : main::{lambda(A)#1}
I now try to abstract composition itself as follows:
template <typename R, typename S, typename T>
std::function<R (T)> compose
( std::function <R (S)> r4s
, std::function <S (T)> s4t )
{ return [=] (T t) { r4s (s4t (t)); }; }
and I get the following errors
main.cpp: In function 'int main()':
main.cpp:44:33: error: no matching function for call to 'compose(C (&)(B), B (&)(A))'
auto c4a = compose (c4b, b4a);
^
main.cpp:44:33: note: candidate is:
main.cpp:34:17: note: template<class R, class S, class T> std::function<R(T)> compose(std::function<R(S)>, std::function<S(T)>)
function<R (T)> compose
^
main.cpp:34:17: note: template argument deduction/substitution failed:
main.cpp:44:33: note: mismatched types 'std::function<R(S)>' and 'C (*)(B)'
auto c4a = compose (c4b, b4a);
which hint that there is a 'pointer-to-function-type' problem; but I thought std::function<...>
was supposed to abstract that away?
Putting in explicit type arguments
auto c4a = compose<C, B, A> (c4b, b4a);
changes the errors, but does not help:
main.cpp: In instantiation of 'std::function<R(T)> compose(std::function<R(S)>, std::function<S(T)>) [with R = C; S = B; T = A]':
main.cpp:44:42: required from here
main.cpp:37:42: error: could not convert '<lambda closure object>compose(std::function<R(S)>, std::function<S(T)>) [with R = C; S = B; T = A]::<lambda(A)>{std::function<C(B)>((*(const std::function<C(B)>*)(& r4s))), std::function<B(A)>((*(const std::function<B(A)>*)(& s4t)))}' from 'compose(std::function<R(S)>, std::function<S(T)>) [with R = C; S = B; T = A]::<lambda(A)>' to 'std::function<C(A)>'
{ return [=] (T t) { r4s (s4t (t)); }; }
Putting in explicit casts
auto c4a = compose (std::function<C(B)>(c4b), std::function<B(A)>(b4a));
or
auto c4a = compose<C, B, A> (std::function<C(B)>(c4b), std::function<B(A)>(b4a));
produces the same errors as above. Clues?