【发布时间】:2016-07-13 02:44:14
【问题描述】:
我正在尝试改造两段遗留代码。其中一个实现了函数调用的超时。到目前为止,它已被用于 C++ 方法并且运行良好。
现在,需要实现与旧 C 库类似的超时。我正在尝试为此使用相同的代码,但遇到了问题。
这是代码的简化版本,是我面临的问题。
uint32_t myFunc1()
{
return 0;
}
uint32_t myFunc2(uint32_t a)
{
return a;
}
int main()
{
uint32_t dummy = 1;
timedCall(myFunc1); //compiles fine.
timedCall(myFunc2, dummy); //compile errors C2672, C2893
}
template <class F, class... Args>
uint32_t timedCall(F &&f, Args&&... a)
{
try
{
std::packaged_task<uint32_t(Args...)> myTask(std::bind(f, a...));
auto res = myTask.get_future();
std::thread(std::move(myTask), a...).detach(); //This is where the issue is.
//Do other stuff
}
catch(...)
{
//handle exceptions
}
return 0; //return something
}
我收到以下错误:
C2672 'std::invoke': no matching overloaded function found
C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
谁能告诉我我做错了什么以及如何解决它?我正在使用 Visual Studio 2015。
【问题讨论】:
-
你能把代码减少到产生错误所需的最大程度吗?您使用的是什么确切版本的 msvc?没有其他错误信息吗?通常它会给出上下文。
-
在 C++11 或 C++14 模式下使用 Clang 编译良好。
-
@Yakk,对不起,应该更具体。我正在使用 Visual Studio 2015。
-
@amol 我确实说“准确”。更新什么?
-
@JohnZwinck,你是对的,它也是用 VS2015 构建的。错误发生在我无意中遗漏的 get_future() 之后的下一行: std::thread(std::move(myTask), a...).detach(); //这就是问题所在。我已经更新了示例代码。
标签: c++ windows visual-studio templates c++11