c++ - C++11 composition with std::function fail -
c++ - C++11 composition with std::function fail -
this question has reply here:
why lambda functions in c++11 not have function<> types? 2 answers(c++11 newbie here) trying composition ideas c++11 lambdas:
#include <iostream> #include <functional> struct { }; struct b { }; struct c { }; b b4a (a a) { b b; homecoming b; } c c4b (b b) { c c; homecoming c; }
i print out types follows (the definition of type_name
bit off-topic, have source of in question building , working online here):
cout << "b4a : " << type_name<decltype(b4a)>() << endl; cout << "c4b : " << type_name<decltype(c4b)>() << endl; auto c4a = [=] (a a) { homecoming c4b (b4a (a)); }; cout << "c4a : " << type_name<decltype(c4a)>() << endl;
which produces next reasonable-looking output:
b4a : b (a) c4b : c (b) c4a : main::{lambda(a)#1}
i seek abstract composition follows:
template <typename r, typename s, typename t> std::function<r (t)> compose ( std::function <r (s)> r4s , std::function <s (t)> s4t ) { homecoming [=] (t t) { r4s (s4t (t)); }; }
and next errors
main.cpp: in function 'int main()': main.cpp:44:33: error: no matching function phone call '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)>' , 'c (*)(b)' auto c4a = compose (c4b, b4a);
which hint there 'pointer-to-function-type' problem; thought std::function<...>
supposed abstract away?
putting in explicit type arguments
auto c4a = compose<c, b, a> (c4b, b4a);
changes errors, 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 here main.cpp:37:42: error: 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)))}' 'compose(std::function<r(s)>, std::function<s(t)>) [with r = c; s = b; t = a]::<lambda(a)>' 'std::function<c(a)>' { homecoming [=] (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 same errors above. clues?
c++11 std::function
not take lambda's of "same type" implicitly, when template type deduction needed. in such case, have explicitly either convert lambda std::function
object or store in std::function
object before using in functional composition. seems current c++ standard cannot handle implicit type conversion , deduction of template types @ same time, because there many combinations of possibilities , associated ambiguity issue.
please see related question here: why lambda functions in c++11 not have function<> types? if interested.
basically, each lambda object own phone call operator ()
. if have 2 lambda's both taking a
, returning b
, of 2 distinct types.
i have wrapper make_function
type conversion unambiguous cases here:
the next might work:
auto c4a = compose<c, b, a> (make_function(c4b), make_function(b4a));
c++ c++11 lambda
Comments
Post a Comment