gpt4 book ai didi

c++ - 标准线程 sleep_for 不适用于某些 chrono::duration

转载 作者:行者123 更新时间:2023-11-30 03:56:01 25 4
gpt4 key购买 nike

我正在使用 VS2012,但我遇到以下示例的问题:

#include <chrono>
#include <thread>

int main()
{
// doesn't compile and I don't understand why:
std::this_thread::sleep_for(std::chrono::duration<double>(0.1));

// I can use this but still I would like to know the reason:
std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100));

return 0;
}

两个持续时间都应该有效。并且可以在不同的上下文中使用它们。

编译错误:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const std::chrono::duration<_Rep>' (or there is no acceptable conversion)
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<_Rep,_Period> &std::chrono::duration<_Rep,_Period>::operator +=(const std::chrono::duration<_Rep,_Period> &)'
1> with
1> [
1> _Rep=__int64,
1> _Period=std::nano
1> ]
1> while trying to match the argument list '(std::chrono::nanoseconds, const std::chrono::duration<_Rep>)'
1> with
1> [
1> _Rep=double
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(164) : see reference to function template instantiation 'xtime std::_To_xtime<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1> i:\prog\.c++\test2\test2\source.cpp(13) : see reference to function template instantiation 'void std::this_thread::sleep_for<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled
1> with
1> [
1> _Nx=0x01,
1> _Rep=double
1> ]
1>
1>Build FAILED.

感谢任何帮助。

最佳答案

这是 VS2012 编译器问题。不能 100% 确定它是否是 this one (thx Praetorian)。但是可以毫无问题地编译 gcc 4.9.2。在我提出要求之前,我应该考虑尝试一下。

关于c++ - 标准线程 sleep_for 不适用于某些 chrono::duration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28704958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com