为什么这段代码会崩溃?
#include <iostream>
#include <functional>
int main(int argc, const char * argv[])
{
std::function<void(int)> function = [](int)
{
};
auto binding = std::bind(function, 10);
std::function<void()> jobFunctor = binding; // crashes here with EXC_BAD_ACCESS
return 0;
}
将绑定(bind)的结果转换为 jobFunctor
时,std::function
构造函数中存在无限堆栈递归。
我正在运行 Mac OS X 10.8.5,我使用 Xcode 5.0.2 使用 libc++ 编译此代码,编译器版本:
LO50F-04-198BX:$ clang++ --version
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
最佳答案
我相当确定这段代码应该可以工作,并且它是 libc++ 实现中的一个缺陷。
我已经在 libc++ 错误跟踪器上为您发布了错误报告。 http://llvm.org/bugs/show_bug.cgi?id=18282
编辑: 正如 Casey 所指出的,这已在 libc++ 中修复。现在您只需等待 Apple 在即将推出的 XCode/OS X 版本中发布更新版本的 libc++。
关于C++11:std::bind 因 lambda 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20685917/