English 中文(简体)
C++ lambda表达方式的寿命如何?
原标题:What is the lifetime of a C++ lambda expression?

(I have read What is the lifetime of lambda-derived implicit functors in C++? already and it does not answer this question.)

我的理解是,C++ lambda syntax 只是糖,因为是一家匿名的班子,有一家叫动经营者和一些州,我理解这个国家的终身要求(决定你是否以参考价值捕获)。 但是,lam子的寿命如何? 下面是<代码>std: 返回的事例将是有益的?

std::function<int(int)> meta_add(int x) {
    auto add = [x](int y) { return x + y; };
    return add;
}

如果是的话,它如何工作? 这对我来说似乎是一种太大的魔 ——我只能想象一下它通过<条码>运作:功能<>。 复制我的全例,视我所掌握情况而定,大体会很重——过去一夫使用<条码>;主要使用光机,并快速复制。 鉴于<代码>std: 功能 类型衰期,也似乎存在问题。

最佳回答

如果你用手工艺更换手工艺品,则寿命是:

struct lambda {
   lambda(int x) : x(x) { }
   int operator ()(int y) { return x + y; }

private:
   int x;
};

std::function<int(int)> meta_add(int x) {
   lambda add(x);
   return add;
}

标的将设于meta_add/code>功能的地方,然后将[包括<代码>x]的轮胎移入收益价值,然后由当地人进行审核,按正常情况加以销毁。 但从功能上退回的物体,只要<代码>d: 功能 持有的物体。 这多长时间显然取决于呼吁的背景。

问题回答

看来,你对<代码>std:tus比lambdas更加混淆。

采用一种称为类型弹性的技术。 这里是快速飞行。

class Base
{
  virtual ~Base() {}
  virtual int call( float ) =0;
};

template< typename T>
class Eraser : public Base
{
public:
   Eraser( T t ) : m_t(t) { }
   virtual int call( float f ) override { return m_t(f); }
private:
   T m_t;
};

class Erased
{
public:
   template<typename T>
   Erased( T t ) : m_erased( new Eraser<T>(t) ) { }

   int do_call( float f )
   {
      return m_erased->call( f );
   }
private:
   Base* m_erased;
};

为什么你们想要抹掉这种类型? 我们想要的类型是否只是<代码>int(*)(float)?

What the type erasure allows is Erased can now store any value that is callable like int(float).

int boring( float f);
short interesting( double d );
struct Powerful
{
   int operator() ( float );
};

Erased e_boring( &boring );
Erased e_interesting( &interesting );
Erased e_powerful( Powerful() );
Erased e_useful( []( float f ) { return 42; } );

这是:

[x](int y) { return x + y; };

相当于:(也可认为法令)

struct MyLambda
{
    MyLambda(int x): x(x) {}
    int operator()(int y) const { return x + y; }
private:
    int x;
};

因此,你们的物体正在返回一个像这样的物体。 后者拥有明确界定的复印件。 因此,似乎非常合理的是,可以正确复制这一职能。

在你颁布的法典中:

std::function<int(int)> meta_add(int x) {
    auto add = [x](int y) { return x + y; };
    return add;
}

The std:Function<int(int)> Object, which is re by the function actual hold a trans instance of the lambda function Object that was assigned to localerracode>add.

When you define a C++11 lambda that captures by-value or by-reference, the C++ compiler automatically generates a unique functional type, an instance of which is constructed when the lambda is called or assigned to a variable. To illustrate, your C++ compiler might generate the following class type for the lambda defined by [x](int y) { return x + y; }:

class __lambda_373s27a
{
    int x;

public:
    __lambda_373s27a(int x_)
        : x(x_)
    {
    }

    int operator()(int y) const {
        return x + y;
    }
};

Then, the meta_add function is essentially equivalent to:

std::function<int(int)> meta_add(int x) {
    __lambda_373s27a add = __lambda_373s27a(x);
    return add;
}

http://www.un.org。 顺便说一句,我不清楚你是否知道这一点,但这是C++11功能currying的一个实例。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签