通过其类型实例化C ++ lambda [英] Instantiating C++ lambda by its type

查看:150
本文介绍了通过其类型实例化C ++ lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种从函数中创建函子的方法。现在我试图包装函数调用lambda函数和实例化它以后。但编译器说比lambda构造函数被删除。
所以有什么办法编译这段代码?或者可能是另一种方式?

  #include< iostream> 

void func()
{
std :: cout< 你好;
}

auto t = [] {func(); };
typedef decltype(t)functor_type;

template< class F>
void functor_caller()
{
F f;
f();
}

int main()
{
functor_caller< functor_type>();
return 0;
}



现在我得到这样的编译器错误:

 错误:使用已删除的函数'< lambda()> ::< lambda()'

错误:闭包类型有一个删除的默认构造函数



在我看来唯一的办法是使用宏:

  #define WRAP_FUNC(f)\ 
struct f ## _ functor \
{\
template< class ... Args> \
auto operator()(Args ... args) - > decltype(f(args ...))\
{\
return f(args ...) ); \
} \
};

然后

  WRAP_FUNC(func); 

然后(在主要)

  functor_caller< func_functor>()


解决方案>

代码没有意义。假设你有一个像这样的捕获lambda:

  {
int n = 0;
auto t = [& n](int a) - > int {return n + = a; };
}

这可能意味着默认构造一个 decltype(t)



正如@Matthieu所说,你可以将lambda包装到 / code> object:

  std :: function< int(int)> F = t; 

或者你可以直接模拟你的调用站点的lambda类型(或任何可调用的实体) :

  template< typename F> 
int compute(int a,int b,F f)
{
return a * f(b); // example
}

用法: int a = 0; for(int i:{1,3,5}){a + = compute(10,i,t); }



如果可能,第二个样式是首选,因为转换为 std :: function 是一个非平凡的,潜在的昂贵的操作,因为通过结果对象的实际函数调用。但是,如果你需要存储异质可调用实体的统一集合,那么 std :: function 可能是最简单和最方便的解决方案。


I want a way to make functor from function. Now I trying to wrap function call by lambda function and instantiate it later. But compiler says than lambda constructor is deleted. So is there any way to compile this code ? Or maybe another way for that ?

#include <iostream>  

void func()
{
    std::cout << "Hello";
}

auto t = []{ func(); };
typedef decltype(t) functor_type;

template <class F>
void functor_caller()
{
    F f;
    f();
}

int main()
{
    functor_caller<functor_type>();
    return 0;
}

Now I get such compiler error:

error: use of deleted function  '<lambda()>::<lambda>()'

error: a lambda closure type has a deleted default constructor

In my opinion the only way is to use macro:

#define WRAP_FUNC(f) \
struct f##_functor       \
{                       \
    template <class... Args >                             \
    auto operator()(Args ... args) ->decltype(f(args...)) \
    {                                                     \
        return f(args...);                                \
    }                                                     \
};

then

WRAP_FUNC(func);

and then (in main)

functor_caller<func_functor>()

解决方案

The code doesn't make sense. Imagine you have a capturing lambda like this:

{
    int n = 0;
    auto t = [&n](int a) -> int { return n += a; };
}

What could it possibly mean to default-construct an object of type decltype(t)?

As @Matthieu suggests, you could wrap the lambda into a function object:

std::function<int(int)> F = t;

Or you could template your call-site directly on the type of the lambda (or any callable entity):

template <typename F>
int compute(int a, int b, F f)
{
    return a * f(b);  // example
}

Usage: int a = 0; for (int i : { 1, 3, 5 }) { a += compute(10, i, t); }

If at all possible, the second style is preferable, since the conversion to std::function is a non-trivial, potentially expensive operation, as is the actual function call through the resulting object. However, if you need to store a uniform collection of heterogeneous callable entities, then std::function may well be the easiest and most convenient solution.

这篇关于通过其类型实例化C ++ lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆