简短的转换,从秒转换为std :: chrono :: steady_clock :: duration? [英] Terse conversion from double seconds to std::chrono::steady_clock::duration?
问题描述
我可以这样做:
double period_in_seconds = 3.4;
auto as_duration = std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(period_in_seconds));
但是我相信您会同意这是非常荒谬的.有麻烦的方法吗?
But I'm sure you'll agree that is quite ridiculous. Is there a terser way?
在明显的使用命名空间std :: chrono
旁边.
推荐答案
有充分的理由狂热地劝阻在文件范围(尤其是在标头中)不使用 using namespace
s.但是,在功能范围内,特别是对于< chrono>
lib,使用名称空间std :: chrono 的是您的朋友.
There is good reason to be fanatic about discouraging use of using namespace
s at file scope, especially in headers. However at function scope, especially with the <chrono>
lib, using namespace std::chrono
is your friend.
这一步可以帮助您:
double period_in_seconds = 3.4;
auto as_duration = std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(period_in_seconds));
收件人:
double period_in_seconds = 3.4;
using namespace std::chrono;
auto as_duration = duration_cast<steady_clock::duration>(duration<double>(period_in_seconds));
当从浮点持续时间转换为整数持续时间时,我建议使用 round
.这不仅会给预期的"答案更多的频率,而且比 duration_cast
的冗长程度略低. std :: chrono :: round
是C ++ 17的新功能.但是您可以从此处或此处并立即开始使用.另外,如果将 {}
用于构造函数,则有助于将对象构造与函数调用区分开,从而使代码更易于阅读:
I recommend the use of round
when casting from a floating point duration to an integral duration. Not only will this give the "expected" answer more often, but it is slightly less verbose than duration_cast
. std::chrono::round
is new with C++17. But you can grab it from various places like here or here and start using it today. Also if you use {}
for constructors it helps distinguish object construction from function calls, making the code a little easier to read:
double period_in_seconds = 3.4;
using namespace std::chrono;
auto as_duration = round<steady_clock::duration>(duration<double>{period_in_seconds});
最后,即使在本地范围内,也可以毫不犹豫地创建自定义的持续时间,以提高可读性:
Finally, don't hesitate to create a custom duration, even if in a local scope to improve readability:
double period_in_seconds = 3.4;
using namespace std::chrono;
using dsec = duration<double>;
auto as_duration = round<steady_clock::duration>(dsec{period_in_seconds});
并且如果由于某种原因(例如样式指南放错了位置)而不能使用命名空间std :: chrono 使用本地函数,那么您可以用更具体的类型别名并使用声明来弥补这一点:
And if for some reason (say a misplaced style guide) you can't using a function-local using namespace std::chrono
, then you can make up for that with more specific type aliases and using declarations:
double period_in_seconds = 3.4;
using dsec = std::chrono::duration<double>;
using duration = std::chrono::steady_clock::duration;
using std::chrono::round;
auto as_duration = round<duration>(dsec{period_in_seconds});
如果此代码旨在用于文件作用域,并且您不希望这些用法在文件作用域(这是完全可以理解的),则只需创建一个函数来封装它们:
If this code is intended to be at file scope and you don't want these usings at file scope (which is perfectly understandable), just create a function to encapsulate them:
constexpr
std::chrono::steady_clock::duration
get_period(double period_in_seconds)
{
using dsec = std::chrono::duration<double>;
using duration = std::chrono::steady_clock::duration;
using std::chrono::round;
return round<duration>(dsec{period_in_seconds});
}
auto constexpr as_duration = get_period(3.4);
上面,我什至演示了这个特定的示例都可以在编译时完成. clang -O3 -S
将上述内容简化为以下程序集:
Above I've even demonstrated that this particular example can all be done at compile time. clang -O3 -S
reduces the above down to the following assembly:
.globl _as_duration ## @as_duration
.p2align 3
_as_duration:
.quad 3400000000 ## 0xcaa7e200
这篇关于简短的转换,从秒转换为std :: chrono :: steady_clock :: duration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!