Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11: std::bind crashes with lambda

Tags:

c++

c++11

lambda

Why does this code crash?

#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;
}

When converting the result of the bind to jobFunctor there is an infinite stack recursion in a std::function constructor.

I am running Mac OS X 10.8.5, I compile this code with Xcode 5.0.2 using libc++, the compiler version:

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
like image 227
Piotr Wach Avatar asked Dec 19 '13 15:12

Piotr Wach


1 Answers

I'm fairly sure that this code should work, and that it is a defect in the implementation of libc++.

I've posted a bug report for you on the libc++ bug tracker. http://llvm.org/bugs/show_bug.cgi?id=18282

Edit: And as Casey has pointed out, this has already been fixed in libc++. Now you'll simply need to wait for Apple to release an updated version of libc++ in some forthcoming version of XCode / OS X.

like image 139
Bill Lynch Avatar answered Oct 31 '22 05:10

Bill Lynch