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

dav*_*sti 7 c++ c++11

我正在尝试使用shared_ptrfrom类启动线程Test,我收到此错误:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:559:2: note: no known conversion for argument 1 from 'std::shared_ptr<Test>' to 'std::shared_ptr<Test>&'

示例代码:

    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
Run Code Online (Sandbox Code Playgroud)

注意:在使用VS2013的Windows中,第一个示例工作正常.

Dav*_*e S 3

这看起来像是您正在使用的 gcc 版本中的一个错误,因为它应该可以工作。看看http://ideone.com/GOQ35M它确实有效

作为解决方法,您可以尝试

std::shared_ptr<Test> test = std::make_shared<Test>();
std::thread th(std::bind(&Test::run, test))
Run Code Online (Sandbox Code Playgroud)