【问题标题】:C++11 composition with std::function fail [duplicate]带有std :: function的C ++ 11组合失败[重复]
【发布时间】:2014-06-18 23:58:15
【问题描述】:

(这里是 C++11 新手) 使用 C++11 lambda 尝试一些组合想法:

#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&lt;...&gt; 应该把它抽象出来?

加入显式类型参数

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

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

【问题讨论】:

标签: c++ c++11 lambda


【解决方案1】:

c++11 std::function 在还需要模板类型推导时,不隐式接受“相同类型”的 lambda。在这种情况下,您必须明确地将您的 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));

【讨论】:

  • 严格来说我不认为这是正确的。据我所知,[](int i, int j) { return i + j; } 语句会生成一个可以隐式转换为std::function&lt;int (int, int)&gt; 的 lambda。我认为当我们尝试将 lambda 隐式转换为带有未绑定模板参数的 std::function 时,问题就出现了。
  • 我现在很感兴趣如何在没有function 转换的情况下声明compose(我确实开始工作了)。我可能应该对那些(失败的)尝试提出另一个问题。
  • 我能得到的最小的是以下template &lt;typename R, typename S, typename T&gt; function&lt;R(T)&gt; compose ( R(r4s)(S) , S(s4t)(T) ) { return [=] (T t) { return r4s (s4t (t)); }; }
  • @ChrisHayden 感谢您的澄清。我已经对声明进行了限定,并说隐式转换仅在同时进行模板类型推导时才是一个问题。我认为这就是造成这里问题的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-14
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多