Lambda和std :: function [英] Lambdas and std::function
问题描述
我试图赶上C ++ 11和所有出色的新功能.我在lambda上有点卡住了.
I'm trying to catch up on C++11 and all the great new features. I'm a bit stuck on lambdas.
这是我能够上班的代码:
Here's the code I was able to get to work:
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <functional>
using namespace std;
template<typename BaseT, typename Func>
vector<BaseT> findMatches(vector<BaseT> search, Func func)
{
vector<BaseT> tmp;
for(auto item : search)
{
if( func(item) )
{
tmp.push_back(item);
}
}
return tmp;
}
void Lambdas()
{
vector<int> testv = { 1, 2, 3, 4, 5, 6, 7 };
auto result = findMatches(testv, [] (const int &x) { return x % 2 == 0; });
for(auto i : result)
{
cout << i << endl;
}
}
int main(int argc, char* argv[])
{
Lambdas();
return EXIT_SUCCESS;
}
我想要的是这样的:
template<typename BaseT>
vector<BaseT> findMatches(vector<BaseT> search, function <bool (const BaseT &)> func)
{
vector<BaseT> tmp;
for(auto item : search)
{
if( func(item) )
{
tmp.push_back(item);
}
}
return tmp;
}
基本上,我想将可能的lambda范围缩小到合理的功能子集. 我想念什么?这有可能吗?我正在使用GCC/G ++ 4.6.
Basically I want to narrow down the possible lambdas to a sensible subset of functions. What am I missing? Is this even possible? I'm using GCC/G++ 4.6.
推荐答案
Stephan T. Lavavej explains why this doesn't work in this video. Basically, the problem is that the compiler tries to deduce BaseT
from both the std::vector
and the std::function
parameter. A lambda in C++ is not of type std::function
, it's an unnamed, unique non-union type that is convertible to a function pointer if it doesn't have a capture list (empty []
). On the other hand, a std::function
object can be created from any possible type of callable entity (function pointers, member function pointers, function objects).
请注意,我个人不理解为什么要将传入函子限制为该特定签名(除了通过std::function
之类的多态函数包装程序进行的间接访问相比,效率要低得多)直接调用函子(甚至可以内联),但这是一个有效的版本.基本上,它禁用std::function
部分的参数推导,并且仅从std::vector
参数推论BaseT
:
Note that I personally don't understand why you would want to limit the incoming functors to that specific signature (in addition to the fact that indirection through a polymorphic function wrapper, like std::function
, is by far more inefficient than a direct call to a functor (which may even be inlined)), but here's a working version. Basically, it disables argument deduction on the std::function
part, and only deduces BaseT
from the std::vector
argument:
template<class T>
struct Identity{
typedef T type;
};
template<typename BaseT>
vector<BaseT> findMatches(vector<BaseT> search,
typename Identity<function<bool (const BaseT &)>>::type func)
{
vector<BaseT> tmp;
for(auto item : search)
{
if( func(item) )
{
tmp.push_back(item);
}
}
return tmp;
}
另一种可能的方法是不直接限制函子类型,而是通过SFINAE间接限制:
Another possible way would be to not restrict the functor type directly, but indirectly through SFINAE:
template<class T, class F>
auto f(std::vector<T> v, F fun)
-> decltype(bool(fun(v[0])), void())
{
// ...
}
如果fun
不接受类型为T&
的参数,或者返回类型不能转换为bool
,则此函数将从重载集中删除. , void()
使f
的返回类型为void
.
This function will be removed from the overload set if fun
doesn't take an argument of type T&
or if the return type is not convertible to bool
. The , void()
makes f
's return type void
.
这篇关于Lambda和std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!