- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
编译器
g++ (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
片段 1(&
捕获)
#include <functional>
int main()
{
std::function<void ()> func = [&] () {
func();
};
return 0;
}
片段 2(func
捕获)
#include <functional>
int main()
{
std::function<void ()> func = [func] () {
func();
};
return 0;
}
两个片段都能编译,但为什么运行第二个片段会导致段错误?
最佳答案
捕获发生在构建 std::function
之前.
因此,您捕获了 std::function<void()> func
的未初始化(甚至不是默认构造的!)拷贝.所有这一切本身就是对 std::function
的捕获是 UB(在构造变量之前复制变量!),调用它甚至会“更加 UB”(调用非构造 std::function
的拷贝!)。
引用案例捕获了对 func
的引用,即使在初始化之前也是允许的,只要它只在初始化后使用。
引用案例的缺点是 lambda 仅在 func
范围内有效.复制一次也是无效的func
超出范围。根据我的经验,这很糟糕。
要执行真正的“全强度”递归 lambda,您需要类似 y 组合器的东西。
这是一个简短的 C++14 y 组合器:
template<class F>
auto y_combinate( F&& f ) {
return [f = std::forward<F>(f)](auto&&...args) {
return f(f, decltype(args)(args)...);
};
}
你传递给它一个 lambda,它期望将对自身的引用作为第一个参数:
std::function<void ()> func = y_combinate( [](auto&& self) {
self(self);
}
);
剩下的就交给它了。
y 组合器要求是因为您无权访问自己的 this
在 lambda 的体内。所以我们加一个。
上面的 y-combinator 只有 90%,因为它没有完美地处理传入的函数对象的 r/l 值和 const-ness。但它会在大部分时间提供服务。
这是一个稍微好一点的 y 组合:
template<class F>
struct y_combinate_t {
F f;
template<class...Args>
decltype(auto) operator()(Args&&...args)const {
return f(*this, std::forward<Args>(args)...);
}
};
template<class F>
y_combinate_t<std::decay_t<F>> y_combinate( F&& f ) {
return {std::forward<F>(f)};
}
这样使用起来更好:
std::function<void ()> func = y_combinate( [](auto&& self) {
self();
}
);
self
现在传进去就不用传了self
调用时本身。
关于c++ - 递归 lambda 和捕获(段错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021455/
可以使用 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
我是一名优秀的程序员,十分优秀!