C++ 将秒作为双精度转换为 std::chrono::duration?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17899684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:32:11  来源:igfitidea点击:

convert seconds as double to std::chrono::duration?

c++c++11chrono

提问by KaiserJohaan

I'm using c++11 <chrono>and have a number of seconds represented as a double. I want to use c++11 to sleep for this duration, but I cannot fathom how to convert it to a std::chrono::durationobject that std::this_thread::sleep_forrequires.

我正在使用 c++11<chrono>并且有一些秒数表示为双精度数。我想在这段时间内使用 c++11 来睡眠,但我无法理解如何将它转换为需要的std::chrono::duration对象std::this_thread::sleep_for

const double timeToSleep = GetTimeToSleep();
std::this_thread::sleep_for(std::chrono::seconds(timeToSleep));  // cannot convert from double to seconds

I've locked at the <chrono>reference but I find it rather confusing.

我已经锁定了<chrono>参考文献,但我发现它相当混乱。

Thanks

谢谢

EDIT:

编辑:

The following gives error:

以下给出错误:

std::chrono::duration<double> duration(timeToSleep );
std::this_thread::sleep_for(duration);

the error:

错误:

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<double,std::ratio<0x01,0x01>>' (or there is no acceptable conversion)
2>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<__int64,std::nano> &std::chrono::duration<__int64,std::nano>::operator +=(const std::chrono::duration<__int64,std::nano> &)'
2>          while trying to match the argument list '(std::chrono::nanoseconds, const std::chrono::duration<double,std::ratio<0x01,0x01>>)'
2>          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<0x01,0x01>>(const std::chrono::duration<double,std::ratio<0x01,0x01>> &)' being compiled
2>          c:\users\johan\desktop\svn\jonsengine\jonsengine\src\window\glfw\glfwwindow.cpp(73) : see reference to function template instantiation 'void std::this_thread::sleep_for<double,std::ratio<0x01,0x01>>(const std::chrono::duration<double,std::ratio<0x01,0x01>> &)' being compiled

采纳答案by Cornstalks

Don't do std::chrono::seconds(timeToSleep). You want something more like:

不要做std::chrono::seconds(timeToSleep)。你想要更像的东西:

std::chrono::duration<double>(timeToSleep)

Alternatively, if timeToSleepis not measured in seconds, you can pass a ratio as a template parameter to duration. See here(and the examples there) for more information.

或者,如果timeToSleep不是以秒为单位测量,您可以将比率作为模板参数传递给duration。有关更多信息,请参见此处(以及此处的示例)。

回答by Alastair Harrison

Making the answer from @Cornstalks a little more generic, you could define a function like this:

使@Cornstalks 的答案更加通用,您可以定义这样的函数:

template <typename T>
auto seconds_to_duration(T seconds) {
    return std::chrono::duration<T, std::ratio<1>>(seconds);
}

This will convert a seconds value of any primitive type to a chrono duration. Use it like this:

这会将任何原始类型的秒值转换为计时持续时间。像这样使用它:

const double timeToSleep = GetTimeToSleep();
std::this_thread_sleep_for(seconds_to_duration(timeToSleep));

回答by slaterade

const unsigned long timeToSleep = static_cast<unsigned long>( GetTimeToSleep() * 1000 );
std::this_thread::sleep_for(std::chrono::milliseconds(timeToSleep));

回答by P0W

std::chrono::milliseconds duration(timeToSleep);
std::this_thread::sleep_for( duration );