从这些SE问题,Passing lambda as function pointer,Lambda as function parameter,Cannot pass lambda function as function reference?,我了解到我可以将无状态的,非捕获的lambdas传递给期望函数指针的函数。因此,我尝试使用模板将T (*bf)(T, T)
类型的二进制函数提升到std::vectors
:
template<typename T>
vector <T> lift_binary (const vector<T> & v1, const vector<T> & v2,
T (* bf)(T, T))
{ auto result = vector<T> ();
result.resize(v1.size());
transform (v1.begin(), v1.end(), v2.begin(), result.begin(), bf);
return result; }
当bf
是命名函数时,这是有效的,例如
template<typename T> T minus (T x, T y) { return x - y; }
template<typename T>
vector<T> operator- (const vector<T> &v1, const vector<T> &v2)
{ return lift_binary (v1, v2, minus); }
int main()
{ auto v1 = vector<double> ({1, 2, 3});
auto v2 = vector<double> ({10, 20, 30});
auto v3 = v1 - v2;
cout << v3[0] << " " << v3[1] << " " < v3[2] << " " << endl;
return 0; }
产生
-9 -18 -27
但是对于以下三个lambda函数中的任何一个都没有用(例如,第二个是未注释的):
template<typename T>
vector<T> operator- (const vector<T> &v1, const vector<T> &v2)
// { return lift_binary (v1, v2, [] (T x, T y) -> T { return x - y; } ); }
{ return lift_binary (v1, v2, [] (T x, T y) { return x - y; } ); }
// { return lift_binary (v1, v2, [] (auto x, auto y) { return x - y; } ); }
// { return lift_binary (v1, v2, minus); }
像这样编译时
g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
编译器无法将参数类型T (*bf)(T, T)
与lambdas的类型匹配:
main.cpp: In instantiation of 'std::vector<_RealType> operator-(const std::vector<_RealType>&, const std::vector<_RealType>&) [with T = double]':
main.cpp:37:20: required from here
main.cpp:26:31: error: no matching function for call to 'lift_binary(const std::vector<double>&, const std::vector<double>&, operator-(const std::vector<_RealType>&, const std::vector<_RealType>&) [with T = double]::<lambda(double, double)>)'
{ return lift_binary (v1, v2, [] (T x, T y) { return x - y; } ); }
^
main.cpp:13:16: note: candidate: template<class T> std::vector<_RealType> lift_binary(const std::vector<_RealType>&, const std::vector<_RealType>&, T (*)(T, T))
vector <T> lift_binary (const vector<T> & v1, const vector<T> & v2, T (* bf)(T, T))
^
main.cpp:13:16: note: template argument deduction/substitution failed:
main.cpp:26:31: note: mismatched types 'T (*)(T, T)' and 'operator-(const std::vector<_RealType>&, const std::vector<_RealType>&) [with T = double]::<lambda(double, double)>'
{ return lift_binary (v1, v2, [] (T x, T y) { return x - y; } ); }
我得到了与其他两个lambda表达式相当的错误。这让我认为没有一种泛型lambda会匹配参数的函数指针类型,但这与我能够找到和读取的内容不相符。我一定是做错了。
这是一个coliru项目,在线提供此样本:
http://coliru.stacked-crooked.com/a/77756b84eb401156
以下是块中的整个代码段:
#include<iostream>
#include<vector>
#include<algorithm>
using std::cout;
using std::endl;
using std::vector;
template<typename T>
vector <T> lift_binary (const vector<T> & v1, const vector<T> & v2, T (* bf)(T, T))
{ auto result = vector<T> ();
result.resize(v1.size());
transform (v1.begin(), v1.end(), v2.begin(), result.begin(), bf);
return result; }
template<typename T>
T minus (T x, T y)
{ return x - y; }
template<typename T>
vector<T> operator- (const vector<T> &v1, const vector<T> &v2)
// { return lift_binary (v1, v2, [] (T x, T y) -> T { return x - y; } ); }
{ return lift_binary (v1, v2, [] (T x, T y) { return x - y; } ); }
// { return lift_binary (v1, v2, [] (auto x, auto y) { return x - y; } ); }
// { return lift_binary (v1, v2, minus); }
template<typename T>
vector<T> operator+(const vector<T> & v1, const vector<T> & v2)
{ return lift_binary (v1, v2, [] (T x, T y) -> T { return x + y; } ); }
int main()
{ auto v1 = vector<double> ({1, 2, 3});
auto v2 = vector<double> ({10, 20, 30});
auto v3 = v1 - v2;
cout << v3[0] << " "
<< v3[1] << " "
<< v3[2] << " " << endl;
return 0; }
修改:
以下作品:
template<typename T>
vector<T> operator- (const vector<T> &v1, const vector<T> &v2)
{ auto result = vector<T> ();
result.resize(v1.size());
transform (v1.begin(), v1.end(), v2.begin(), result.begin(),
[] (T x, T y) { return x - y; } );
return result; }
并且原始问题的全部动机只是允许我在这个模式上进行抽象,以便在实现其他二元运算符(例如+
,{{}时干我(不要重复你的(我的)自我) 1}}等等。
我希望能够通过编写一个以*
作为参数的高阶函数lift_binary
来抽象出lambda表达式,因为我认为这是lambda的类型。