使用带有lambda的decltype和std :: function

时间:2023-02-03 18:01:56

This works ...

这有效......

auto x = 4;
typedef decltype(x) x_t;
x_t y = 5;

... so why doesn't this?

......为什么不呢?

int j = 4;  
auto func = [&] (int i) { cout << "Hello: i=" << i << "  j=" << j << endl;};
typedef decltype(func) lambda_t;
lambda_t func2 = [&] (int i) { cout << "Bye: i=" << i << "  j=" << j << endl;};

... and how would I declare lambda_t manually using std::function?

...以及如何使用std :: function手动声明lambda_t?

3 个解决方案

#1


14  

... so why doesn't this [work]?

...那为什么不这[工作]?

Because each lexical instance of a lambda has a different type. It does not matter if the same characters are used.

因为lambda的每个词法实例都有不同的类型。如果使用相同的字符则无关紧要。

.. and how would I declare lambda_t manually using std::function?

..以及如何使用std :: function手动声明lambda_t?

The lambda takes an int argument and does not return anything... Therefore:

lambda采用int参数并且不返回任何内容......因此:

typedef std::function<void(int)> lambda_t;

#2


7  

Lambda types are unutterable (cannot be named), which is the reason you cannot do what you are asking. Besides that, each lambda is of a different type, so even if you could name the type, you would not be able to assign the second lambda to the first. If you think of the lambda syntax as a shortcut for a function object that becomes clearer: the member operator() is different for each lambda and thus they are of different types.

Lambda类型是不可知的(无法命名),这就是你不能做你要求的原因。除此之外,每个lambda都是不同的类型,所以即使你可以命名类型,你也无法将第二个lambda分配给第一个lambda。如果您认为lambda语法作为函数对象的快捷方式变得更加清晰:成员operator()对于每个lambda都是不同的,因此它们具有不同的类型。

You can, on the other hand assign a lambda to a std::function<> object of the appropriate signature, which in your case would be std::function<void(int)>.

另一方面,您可以将lambda分配给相应签名的std :: function <>对象,在您的情况下,它将是std :: function (int)>

#3


0  

Here's some solid proof that this doesn't work. Similar scenario:

这里有一些确凿的证据证明这不起作用。类似情况:

int foo = 3;
int bar = 3;

std::cout << (typeid(foo).hash_code() == typeid(bar).hash_code()); // prints one -- obviously - they are the same type

Now, lets use the exact same code, but with lambdas. What do you think the response will be.

现在,让我们使用完全相同的代码,但使用lambdas。您认为应对措施是什么?

  auto foo = [](){std::cout << "HELLO\n"; };

  auto bar = [](){std::cout << "HELLO\n"; };

  std::cout << (typeid(foo).hash_code() == typeid(bar).hash_code()); // prints 0 -- different type even though they are declared exactly the same.

#1


14  

... so why doesn't this [work]?

...那为什么不这[工作]?

Because each lexical instance of a lambda has a different type. It does not matter if the same characters are used.

因为lambda的每个词法实例都有不同的类型。如果使用相同的字符则无关紧要。

.. and how would I declare lambda_t manually using std::function?

..以及如何使用std :: function手动声明lambda_t?

The lambda takes an int argument and does not return anything... Therefore:

lambda采用int参数并且不返回任何内容......因此:

typedef std::function<void(int)> lambda_t;

#2


7  

Lambda types are unutterable (cannot be named), which is the reason you cannot do what you are asking. Besides that, each lambda is of a different type, so even if you could name the type, you would not be able to assign the second lambda to the first. If you think of the lambda syntax as a shortcut for a function object that becomes clearer: the member operator() is different for each lambda and thus they are of different types.

Lambda类型是不可知的(无法命名),这就是你不能做你要求的原因。除此之外,每个lambda都是不同的类型,所以即使你可以命名类型,你也无法将第二个lambda分配给第一个lambda。如果您认为lambda语法作为函数对象的快捷方式变得更加清晰:成员operator()对于每个lambda都是不同的,因此它们具有不同的类型。

You can, on the other hand assign a lambda to a std::function<> object of the appropriate signature, which in your case would be std::function<void(int)>.

另一方面,您可以将lambda分配给相应签名的std :: function <>对象,在您的情况下,它将是std :: function (int)>

#3


0  

Here's some solid proof that this doesn't work. Similar scenario:

这里有一些确凿的证据证明这不起作用。类似情况:

int foo = 3;
int bar = 3;

std::cout << (typeid(foo).hash_code() == typeid(bar).hash_code()); // prints one -- obviously - they are the same type

Now, lets use the exact same code, but with lambdas. What do you think the response will be.

现在,让我们使用完全相同的代码,但使用lambdas。您认为应对措施是什么?

  auto foo = [](){std::cout << "HELLO\n"; };

  auto bar = [](){std::cout << "HELLO\n"; };

  std::cout << (typeid(foo).hash_code() == typeid(bar).hash_code()); // prints 0 -- different type even though they are declared exactly the same.