- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设复制变量有必要的副作用。我想声明一个复制变量但不使用变量的 lambda。执行此操作的最低要求是多少?
Copiable copyable;
auto lambda1 = [=](){};
auto lambda2 = [copyable](){};
auto lambda3 = [=](){ copyable; }
auto lambda4 = [=](){ volatile copy = copyable; }
lambda1
使用隐式捕获,并且由于正文中没有提到 copyable
,我不相信它实际上复制了它。
lambda2
使用显式捕获,它似乎根据 this ,它应该通过复制捕获。是否允许编译器省略拷贝?参见 this对此进行另一次讨论。
lambda3
使用隐式捕获,但主体提到了 copyable
。这是否构成 copyable
的odr-use?
lambda4
使用隐式捕获并强制另一个 volatile
拷贝。我确信这确实有效,但它所做的拷贝比最小拷贝更多。
激励案例:我需要在完成任意数量的 lambda 调用后运行清理,可能在不同的线程中。我可以通过将 std::shared_ptr
与运行清理的自定义删除器一起使用,并以某种方式将其传递给每个 lambda 来实现。然后,当所有共享指针超出范围时,将运行清理。
编辑:lambda3
和 lambda4
缺少用于隐式捕获的 =
。
最佳答案
What's the minimum required to do this?
按值显式捕获,如 lambda2
。
lambda1
uses implicit capture, and since the body doesn't mention copyable, I don't believe it actually copies it.
没错。只有当变量在 lambda 中被odr-used时,它们才会被隐式捕获。
lambda2
uses explicit capture, and it seems according to this, it should capture by copy.
没错。任何显式捕获的变量都将被捕获,无论它们是否被使用。这是您要确保捕获对象的操作。
Is the compiler allowed to elide the copy? See this for another discussion of this.
没有。如果捕获了一个变量,那么它就被捕获了。该链接并没有真正“讨论”那个;唯一的答案确认是这种情况,并使用标准中的适当措辞。
lambda3
uses implicit capture but the body mentions copyable. Does this constitute an odr-use of copyable?
是的。 odr-use 的定义是
A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression and the lvalue-to-rvalue conversion is immediately applied.
并且异常不适用,因为它不是常量(因此不能出现在常量表达式中)。 (但请注意,这是错误的格式,因为没有默认捕获。)
lambda4
uses implicit capture and forces another volatile copy. I'm sure this will actually work, but it's doing more copies than the minimum.
的确;您通过使用该值强制隐式捕获,并强制执行额外的拷贝。这是不必要的,因为 lambda2
会做你想做的事。
关于c++ - 强制 C++11 lambda 捕获变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780002/
可以使用 lambda 和函数创建有序对(Lisp 中的缺点),如 Use of lambda for cons/car/cdr definition in SICP 所示。 它也适用于 Python
我正在尝试从另一个调用一个 AWS lambda 并执行 lambda 链接。这样做的理由是 AWS 不提供来自同一个 S3 存储桶的多个触发器。 我创建了一个带有 s3 触发器的 lambda。第一
根据以下源代码,常规 lambda 似乎可以与扩展 lambda 互换。 fun main(args: Array) { val numbers = listOf(1, 2, 3) f
A Tutorial Introduction to the Lambda Calculus 本文介绍乘法函数 The multiplication of two numbers x and y ca
我想弄清楚如何为下面的表达式绘制语法树。首先,这究竟是如何表现的?看样子是以1和2为参数,如果n是 0,它只会返回 m . 另外,有人可以指出解析树的开始,还是一个例子?我一直找不到一个。 最佳答案
在 C++0x 中,我想知道 lambda 函数的类型是什么。具体来说: #include type1 foo(int x){ return [x](int y)->int{return x * y
我在其中一个职位发布中看到了这个问题,它询问什么是 lambda 函数以及它与高阶函数的关系。我已经知道如何使用 lambda 函数,但不太自信地解释它,所以我做了一点谷歌搜索,发现了这个:What
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
我正在处理 MyCustomType 的实例集合如下: fun runAll(vararg commands: MyCustomType){ commands.forEach { it.myM
Brian 在他对问题 "Are side effects a good thing?" 的论证中的前提很有趣: computers are von-Neumann machines that are
在 Common Lisp 中,如果我希望两个函数共享状态,我将按如下方式执行 let over lambda: (let ((state 1)) (defun inc-state () (in
Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme. 我不知道实际上该怎么做! ((lambda (x y) (+ x x
作为lambda calculus wiki说: There are several possible ways to define the natural numbers in lambda cal
我有一个数据类,我需要初始化一些 List .我需要获取 JsonArray 的值(我使用的是 Gson)。 我做了这个函数: private fun arrayToList(data: JsonAr
((lambda () )) 的方案中是否有简写 例如,代替 ((lambda () (define x 1) (display x))) 我希望能够做类似的事情 (empty-lam
我在 Java library 中有以下方法: public void setColumnComparator(final int columnIndex, final Comparator colu
我正在研究一个函数来计算国际象棋游戏中棋子的有效移动。 white-pawn-move 函数有效。当我试图将其概括为任一玩家的棋子 (pawn-move) 时,我遇到了非法函数调用。我已经在 repl
考虑这段代码(在 GCC 和 MSVC 上编译): int main() { auto foo = [](auto p){ typedef decltype(p) p_t;
我正在阅读一个在 lambda 内部使用 lambda 的片段,然后我想通过创建一个虚拟函数来测试它,该函数从文件中读取然后返回最大和最小数字。 这是我想出来的 dummy = lambda path
我是一名优秀的程序员,十分优秀!