std::线程错误C2893在VS2015

std::thread Error C2893 in VS2015

本文关键字:VS2015 C2893 错误 线程 std      更新时间:2023-10-16
// C2893.cpp
// OK in VS2013, Error in VS2015
// compile with: /EHsc
#include <thread>
//
template <typename T_app>
struct instance
{
    void load() {;}
};
//
template <typename T_app>
struct scene
{
    T_app *p_app;
    void thread_load() {
        std::thread(&instance<T_app>::load, std::ref(p_app->app_inst) ).detach();
    }
};
//
struct app
{
    instance<app> app_inst;
    scene<app> app_scene;
};
//
int main() {
    app my_app;
    my_app.app_scene.p_app = &my_app;
    // OK in VS2013, Error in VS2015
    my_app.app_scene.thread_load();
    return 0;
}

你好,我刚刚更新到VS2015, VS2013中的代码是可以的,如何纠正thread_load()的错误?我阅读了Visual c++ 2015中的突破性变化,它必须是非类型模板参数问题,但我找不到正确的方法。谢谢。

错误在这里std::ref(p_app->app_inst)。在这种情况下,std::thread的第二个参数应该是instance*,但p_app->app_inst只是instance类。

所以答案是std::thread(&instance<T_app>::load, &p_app->app_inst ).detach();没有任何lambda函数