错误:'Failed to specialize function template' C2893 'std::invoke'

Error: 'Failed to specialize function template' C2893 'std::invoke'

本文关键字:C2893 std invoke template to Failed 错误 specialize function      更新时间:2023-10-16

我正在Visual Studio 2013中编写MFC程序,并且不断收到以下两个错误

Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'

Error C2672 'std::invoke': no matching overloaded function found

该错误与文件xthread第238行有关

我对c++/MFC还很陌生,我正在尝试编写一个在系统后台运行的函数。

这是我正在使用的代码:

void task1(ExperimentTab& dlg)
{
    while (true)
    {
        CString showtime = CTime::GetCurrentTime().Format("%H:%M:%S");
        int x = dlg.m_showTime.GetWindowTextLengthA();
        dlg.m_showTime.SetWindowTextA(_T(""));
        dlg.m_showTime.ReplaceSel(showtime, 0);
    }
}
void mainThread()
{
    std::thread t1(task1);
    t1.join();
}

然后按下按钮开始计时,但同样的按钮也用于停止计时。

函数task1只接受一个参数(用作线程体),但在t1构造函数中不传递任何参数。编译器无法在没有方法参数的情况下创建调用task1函数的std::invoke

要修复它,请调用类似的构造函数:std::thread t1(task1, std::ref(dlg));,其中dlgExperimentTabstd::ref确保dlg将通过引用传递给线程。

BTW:从其他线程更新MFC组件可能会导致一些数据争用。此外,while(true)线程将通过每秒多次更新秒分辨率的计时器来消耗100%的CPU。