将lambda表达式视为可调用对象的"语法糖",可以表达未命名的基础类型吗?
一个例子:
struct gt {
bool operator() (int l, int r) {
return l > r;
}
} ;
Run Code Online (Sandbox Code Playgroud)
现在,[](int l, int r) { return l > r; }是上述代码的优雅替代(加上gt的可调用对象的必要创建),但有没有办法表达gt(类型)本身?
一个简单的用法:
std::set<int, gt> s1; // A reversed-order std::set
// Is there a way to do the same using a lambda?
std::set<int, some-magic-here-maybe([](int l, int r) { return l > r; }) > s2;
Run Code Online (Sandbox Code Playgroud) 在某些情况下,希望能够键入 - 擦除可调用的(例如函数,函数指针,带有operator()lambda的对象实例mem_fn),例如在使用带有C++ 11 lambdas的Boost适配器中,其中可复制的和可默认构造的类型是必需的.
std::function将是理想的,但似乎没有办法自动确定用于实例化类模板的签名std::function.是否有一种简单的方法来获取任意可调用的函数签名和/或将其包装在适当的std::function实例化实例(即make_function函数模板)中?
具体来说,我正在寻找一个或另一个
template<typename F> using get_signature = ...;
template<typename F> std::function<get_signature<F>> make_function(F &&f) { ... }
Run Code Online (Sandbox Code Playgroud)
这样make_function([](int i) { return 0; })返回std::function<int(int)>.显然,如果一个实例可以使用多个签名(例如,具有多个,模板或默认参数operator()的对象)进行调用,那么预计这不会起作用.
尽管非过度复杂的非Boost解决方案是优选的,但Boost很好.
编辑:回答我自己的问题.
std::function<>几乎任何可调用的东西都是一个有用的包装器,包括自由函数,lambda,仿函数,成员函数,结果std::bind.但是,在创建a时std::function<>,必须明确指定函数签名(取自此处)
struct Foo {
Foo(int num) : num_(num) {}
void print_add(int i) const { std::cout << num_+i << '\n'; }
int num_;
};
void print_num(int i)
{ std::cout << i << '\n'; }
struct PrintNum {
void operator()(int i) const
{ std::cout << i << '\n'; }
};
// store a free function
std::function<void(int)> f_display = print_num;
// store a lambda
std::function<void()> f_display_42 = []() { print_num(42); };
// store the result of …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种标准方法来获取任何给定lambda的参数的类型签名(即返回类型和类型)?
我问的原因是我一直想知道auto声明中的类型究竟是什么样的auto l =[](int x,int y)->int{return x+y;}.在其他用例中auto,对于较长的类型名称而言,它是一种便利和更短的替代方案.但是对于lambdas,是否有另一种方法来声明lambda变量?
我的理解是标准lambda只不过是一个函数对象,它是它自己的类型.因此,即使两个lambda具有相同的返回类型和参数类型,它们仍然是两个不同的,不相关的类/仿函数.但是有一种方法可以捕捉它们在类型签名方面相同的事实吗?
我认为我正在寻找的类型签名可能类似于std::function<>正确类型的对象.
更有用/涉及的问题是,如果可以提取类型签名,则可以编写一般包装函数将任何lambda函数转换std::function为相同类型签名的对象.
C++ 11同时具有lambda和std :: function <>,但不幸的是,它们有不同的类型.一个结果是,人们无法直接在高阶函数中使用lambda,例如lisp中的map.例如,在以下代码中
#include <vector>
#include <functional>
using namespace std;
template <typename A,typename B>
vector<B> map(std::function<B (A)> f, vector<A> arr) {
vector<B> res;
for (int i=0;i<arr.size();i++) res.push_back(f(arr[i]));
return res;
}
int main () {
vector<int> a = {1,2,3};
map([](int x) -> int { return x;},a); //not OK
auto id_l = [](int x) -> int { return x;};
map(id_l,a); //not OK;
function<int (int)> id_f = id_l;
map(id_f,a); //OK
return 0;
}
Run Code Online (Sandbox Code Playgroud)
,直接使用lambda作为main()的第2行将无法正常工作.g++ -std=c++11 testfunc.cpp返回`... testfunc.cpp:14:37:注意:'main():: __ lambda0'不是从'std …
在下面带有参数包和a的模板函数中,ReturnType如果我省略了最后一个参数,为什么编译器没问题ReturnType,而如果我明确给出最后一个类型参数,则给我一个错误(关于歧义).
谢谢.
#include <functional>
using namespace std;
template<typename... Args, typename ReturnType>
auto make_function(ReturnType(*p)(Args...))
-> std::function<ReturnType(Args...)> {
return {p};
}
int foo1(int x, int y, int z) { return x + y + z;}
float foo1(int x, int y, float z) { return x + y + z;}
int main() {
auto f0 = make_function<int,int,int>(foo1); //OK
//auto f1 = make_function<int,int,int,int>(foo1); //not OK
// test33.cpp:15:48: error: no matching function for call to
// 'make_function(<unresolved overloaded function type>)'
return …Run Code Online (Sandbox Code Playgroud)