带有std :: function的C ++ 11组合失败

时间:2014-06-18 23:58:16

标签: c++ c++11 lambda

(C ++ 11新手在这里) 用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;   }

我现在打印出类型如下(type_name的定义有点偏离主题,但我有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;

产生以下合理的输出:

b4a : B (A)
c4b : C (B)
c4a : main::{lambda(A)#1}

我现在尝试按如下方式抽象构图本身:

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));  };  }

我收到以下错误

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);

暗示存在“指向函数类型”的问题;但是我认为std::function<...>应该将其抽象出来?

使用显式类型参数

auto c4a = compose<C, B, A> (c4b, b4a);

更改错误,但没有帮助:

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));  };  }

进行显式演员

auto c4a = compose (std::function<C(B)>(c4b), std::function<B(A)>(b4a));

auto c4a = compose<C, B, A> (std::function<C(B)>(c4b), std::function<B(A)>(b4a));

产生与上述相同的错误。线索?

1 个答案:

答案 0 :(得分:1)

c ++ 11 std::function不接受&#34;相同类型&#34;的lambda&#34;隐含地,还需要模板类型推导。在这种情况下,您必须明确地将lambda转换为std::function对象,或者在将其用于功能组合之前将其存储在std::function对象中。似乎当前的c ++标准不能同时处理隐式类型转换和模板类型的推导,可能是因为存在太多可能性组合以及相关的模糊性问题。

如果您有兴趣,请在此处查看我的相关问题:why do lambda functions in c++11 not have function<> types?

基本上,每个lambda就像一个具有自己的调用操作符()的对象。因此,即使你有两个lambda都采用A并返回B,它们也有两种不同的类型。

我有一个包装器make_function来为明确的案例进行类型转换here

以下可能有效:

auto c4a = compose<C, B, A> (make_function(c4b), make_function(b4a));