在线程中特化函数模板失败
Failed to specialize function template in threads
我试图在c++中实现一个线程,当我拍照时,它将用于控制,但是这个简单的代码给了我一个奇怪的错误,我无法理解。这是我的代码:
#include <iostream>
#include <fstream>
#include <thread>
using namespace std;
void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart);
int main() {
bool takePhoto, threadStart;
int seconds = 1;
thread t(counter, seconds, takePhoto, threadStart);
//Some code here
}
void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart) {
while (flagThreadStart) {
this_thread::sleep_for(chrono::seconds(seconds));
flagTakePhoto = true;
}
terminate();
}
错误:
1>------ Build started: Project: Proyecto para pruebas OPENCV, Configuration:
Release x64 ------
1> Main.cpp
1>Main.cpp(46): warning C4244: 'initializing': conversion from '__int64' to 'int', possible loss of data
1>C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(238): note: With the following template arguments:
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(238): note: '_Callable=void (__cdecl *)(int,bool &,bool &)'
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(238): note: '_Types={int, bool, bool}'
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1> _Ty=size_t
1> ]
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1> _Ty=size_t
1> ]
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethr/xthread(256): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> C:Program Files (x86)Microsoft Visual Studio 14.0VCincludethread(52): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1> ]
1> Main.cpp(136): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(int,bool &,bool &),int&,bool&,bool&,void>(_Fn,int &,bool &,bool &)' being compiled
1> with
1> [
1> _Fn=void (__cdecl &)(int,bool &,bool &)
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
更奇怪的是,我有一个类似的代码在其他程序中编译没有错误!有人知道这段代码有什么问题吗?谢谢!
std::thread
构造函数将构造它的所有对象并调用函数,就像这样:
template <class T>
typename decay<T>::type decay_copy(T&& v) {
return std::forward<T>(v);
}
使得
的参数类型thread t(counter, seconds, takePhoto, threadStart);
为int
, bool
, bool
。由于不能调用counter(int, bool, bool)
(它接受左值引用),因此该构造函数是病态的。
为了传递引用,你需要用std::reference_wrapper<T>
包装你的参数。该类型可以隐式转换为T&
,因此调用counter(int, std::reference_wrapper<bool>, std::reference_wrapper<bool>)
是有效的。简而言之,标准提供了std::ref
,因此您只需执行以下操作:
thread t(counter, seconds, std::ref(takePhoto), std::ref(threadStart));
相关文章:
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 将重载的成员函数传递给函数模板
- 作为模板参数的成员函数指针在继承的成员函数上失败,如何以及为什么?
- 可变参数模板函数重载失败
- 在 GCC 中工作的外行构造函数模板在 Clang 中失败
- 为什么模板参数推导在重载函数时失败?
- 函数模板参数推断使用 std::Optional 失败
- 模板作为函数模板的参数 - 演绎失败
- C++:转发模板成员函数调用失败
- 函数模板实例化失败
- 使用函数指针作为参数匹配模板失败
- 当参数是引用时,可变模板构造函数选择失败
- 使用std::函数替换模板失败
- 使用Lambda函数转换模板失败
- 在线程中特化函数模板失败
- c++错误C2893:特化函数模板失败
- 使模板函数编译失败,并返回错误消息
- 可以实例化 C++ 类模板,但具有相同模板参数的函数模板实例化失败
- 为什么函数模板调用类模板的静态方法模板时编译失败
- 双模板函数重载失败