I am trying to port my code from linux to windows. However with Visual Studio my code crashes with the following error:
Microsoft C++ exception: std::bad_function_call at memory location
This is my code:
#include <functional>
class Foo {
public:
Foo(int) : m_deleter{ []() {} } {}
Foo(const Foo &) = delete;
Foo(Foo &&) = default;
Foo & operator=(const Foo &) = delete;
Foo & operator=(Foo &&) = default;
~Foo()
{
m_deleter();
}
private:
std::function<void()> m_deleter;
};
int main() {
Foo foo(1);
Foo bar(2);
std::swap(foo, bar);
}
It crashes when I use std::swap
. In linux it worked flawlessly.
Weirdly enough, when I try to compile it online via GCC it doesn't work either. What am I doing wrong and why does at work at home with Clang (3.5).
EDIT: It turns out it crashes with Visual Studio 2015 and GCC 4.9.2, but not with Clang 3.5.