使用 std::thread 和 std::shared_ptr 时编译器错误

7
我将尝试使用来自类“Test”的“shared_ptr”启动线程,但我遇到了这个错误:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2:注意:从'std :: shared_ptr '到 'std :: shared_ptr &'的参数1没有已知的转换
示例代码:
    std::shared_ptr<Test> test = std::make_shared<Test>();
    std::thread th(&Test::run, test); // Compiler error


    Test* test2 = new Test;
    std::thread th(&Test::run, test2); // okay

注意:在使用VS2013的Windows系统中,第一个例子可以正常运行。

2
这看起来像是您正在使用的gcc版本中的一个错误。std::thread th(std::bind(&Test::run, test))是否更好(可能不会,因为它们可能使用一些共同的内部代码)? - Dave S
多奇怪... - Kerrek SB
std::bind工作正常!谢谢。 - davidaristi
1
在GCC 4.7上无法工作,但在4.8上可以。链接1链接2 - T.C.
1
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56505 - T.C.
1个回答

3
这似乎是您使用的gcc版本中的错误,因为它应该可以工作。从http://ideone.com/GOQ35M看来,它确实可以工作。
作为解决方法,您可以尝试:
std::shared_ptr<Test> test = std::make_shared<Test>();
std::thread th(std::bind(&Test::run, test))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接