从lambda获取捕获的变量? [英] Get captured variables from lambda?

查看:371
本文介绍了从lambda获取捕获的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果有一种方法来获取lambda的捕获变量的类型/值? - 使用场景会是类似的;

I was wondering, if there's a way to get the types/values of the captured variables of a lambda? - The usage scenario would be something alike;

int a = 5;
auto lamb = [a](){ return a; };
static_assert(std::is_same<typename get_capture_type<0>(lamb)::type, int>::value, "");
assert(get_capture_value<0>(lamb) == 5)

注意: get_capture _ *< N>(lambda)应当明显导致编译器错误。 #captured_variables 。

Note: get_capture_*<N>(lambda) should obviously result in a compiler error, when N > #captured_variables.

我需要的只是一种方法来访问捕获,如果可能的话。

What I need is actually just a way to access the captures somehow, if possible. That is, I can do the template meta-programming myself.

推荐答案

这是不可能的设计


5.1.2 [expr.prim.lambda]

15 [ 。]对于通过复制捕获的每个实体,在闭包类型中声明一个未命名的非静态数据成员。这些成员的声明顺序未指定。 [...]

16 [...]未指定在引用捕获的实体的闭包类型中是否声明了其他未命名的非静态数据成员。

5.1.2 [expr.prim.lambda]
15 [...] For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified. [...]
16 [...] It is unspecified whether additional unnamed non-static data members are declared in the closure type for entities captured by reference.

捕获的变量未命名(或至少有名字不可能由凡人说出),并且它们的声明顺序是故意未指定的。

Captured variables are unnamed (or at least have names that are unspeakable by mortals) and their declaration order is deliberately unspecified. By-reference captures may not even exist in the closure type.

您仍然不希望这样做。你可能会认为你做,但你不是真的。

You don't want to do this anyway. You may think you do, but you don't really.

这篇关于从lambda获取捕获的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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