【发布时间】:2015-03-09 08:00:39
【问题描述】:
我正在尝试将我的代码从 linux 移植到 windows。但是使用 Visual Studio 我的代码崩溃并出现以下错误:
Microsoft C++ 异常:内存位置的 std::bad_function_call
这是我的代码:
#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);
}
当我使用std::swap 时它崩溃了。在 linux 中它可以完美运行。
奇怪的是,当我尝试通过 GCC 发送 compile it online 时,它也不起作用。我做错了什么以及为什么在家里使用 Clang (3.5) 工作。
编辑:结果它在 Visual Studio 2015 和 GCC 4.9.2 中崩溃,但在 Clang 3.5 中没有。
【问题讨论】:
-
@Ron Tang:如果我将其设为默认值,它仍然会崩溃。
-
en... , 使用我的 linux gcc 也有同样的错误!
-
在 linux 中它可以完美运行。您使用哪个编译器?
-
@Ron Tang:如我的问题所述,我使用 Clang 3.5。
-
在 ubuntu 上使用 gcc 4.9.1 崩溃,出现相同的异常
标签: c++ visual-studio gcc clang