lambda表达式:n3290草稿 [英] Lambda expressions : n3290 draft

查看:133
本文介绍了lambda表达式:n3290草稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自n3290 ISO草稿的点:$ b​​ $ b Lambda表达式:第5.1.2节第6段:

 没有
lambda-capture的lambda表达式的闭包类型具有公共非虚拟非显式const
转换函数,指向具有相同
参数的函数的指针,返回类型为闭包类型的函数
call operator这个转换返回的值
函数应该是一个函数的地址,当
被调用时,与调用闭包有相同的效果
type函数调用操作符。

任何人可以用一个例子解释这一点吗?

解决方案

简短回答



这只是意味着lambdas不捕获任何东西转换为具有相同签名的函数指针:

  auto func = [](int x){return x * }; 
int(* func_ptr)(int)= func; //法律。

int y = func_ptr(2); // y is 4.

并且捕获使其为非法:

  int n = 2; 
auto func = [=](int x){return x * n; };
int(* func_ptr)(int)= func; // illegal,func captures n

长答案

Lambdas是创建函子的简写:

  auto func = [] x){return x * 2; }; 

等效于:

  struct func_type 
{
int operator()(int x)const {return x * 2; }
}

func_type func = func_type();

在这种情况下, func_type 类型和operator()是函数调用操作符。当你获取lambda的地址时,就好像你声明了 operator() static,并取其地址,就像任何其他函数:

  struct func_type 
{
static int f(int x){return x * 2; }
}

int(* func_ptr)(int)=& func_type :: f;

捕获变量后,它们成为 func_type operator()取决于这些成员,因此不能设为静态:

  struct func_type 
{
int const m_n;

func_type(int n):m_n(n){}
int operator()(int x)const {return x * m_n; }
}

int n = 2;
auto func = func_type(n);

普通函数没有成员变量的概念。保持这个想法,lambdas只能被视为普通函数,如果他们也没有成员变量。


A point from n3290 ISO draft: Lambda expressions : section 5.1.2, para 6:

         "The closure type for a lambda-expression with no 
      lambda-capture has a public non-virtual non-explicit const
      conversion function to pointer to function having the same
      parameter and return types as the closure type’s function
      call operator. The value returned by this conversion
      function shall be the address of a function that, when
      invoked, has the same effect as invoking the closure
      type’s function call operator."

Can any one explain this point with an example please ?

解决方案

The short answer

This just means that lambdas not capturing anything can be converted into a function pointer with the same signature:

auto func = [](int x) { return x * 2; };
int (*func_ptr)(int) = func; // legal.

int y = func_ptr(2); // y is 4.

And a capture makes it illegal:

int n = 2;
auto func = [=](int x) { return x * n; };
int (*func_ptr)(int) = func; // illegal, func captures n

The long answer

Lambdas are shorthand to create a functor:

auto func = [](int x) { return x * 2; };

Is equivalent to:

struct func_type
{
    int operator()(int x) const { return x * 2; }
}

func_type func = func_type();

In this case func_type is the "closure type" and operator() is the "function call operator". When you take the address of a lambda, it is as if you declared the operator() static and take its address, like any other function:

struct func_type
{
    static int f(int x) { return x * 2; }
}

int (*func_ptr)(int) = &func_type::f;

When you have captured variables, they become members of func_type. operator() depends on these members, so it can't be made static:

struct func_type
{
    int const m_n;

    func_type(int n) : m_n(n) {}
    int operator()(int x) const { return x * m_n; }
}

int n = 2;
auto func = func_type(n);

An ordinary function has no notion of member variables. Keeping with this thought, lambdas can only be treated as an ordinary function if they also have no member variables.

这篇关于lambda表达式:n3290草稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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