chrono :: month和chrono :: months有什么区别 [英] What is the difference between chrono::month and chrono::months

查看:105
本文介绍了chrono :: month和chrono :: months有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 20计时类型/值 month {7} months {7}

What is the difference between the C++20 chrono types/values month{7} and months{7}? Isn't it confusing to have two such similar names?

推荐答案

是的,同时拥有两个<$ c $可能会造成混淆首次遇到此库时,c> month months 。但是,此库中有一致的命名约定,以帮助减少这种混淆。这样做的好处是可以清楚区分不同的语义,同时保留简短的直观名称。

Yes, it can be confusing to have both month and months when first encountering this library. However there are consistent naming conventions in this library to help reduce that confusion. And the benefit is having a clear separation of distinct semantics while retaining short intuitive names.

所有预定义 chrono :: duration 类型都是复数:

All "predefined" chrono::duration types are plural:


  • 纳秒

  • 微秒

  • 毫秒


  • 分钟

  • 小时



  • 个月


  • nanoseconds
  • microseconds
  • milliseconds
  • seconds
  • minutes
  • hours
  • days
  • weeks
  • months
  • years

所以个月 chrono :: duration 类型


using months = duration<signed integer type of at least 20 bits,
                         ratio_divide<years::period, ratio<12>>>;

它恰好是 1 / 12

And it is exactly 1/12 of years.

static_assert(12*months{1} == years{1});

您可以像这样打印出来:

You can print it out like this:

cout << months{7} << '\n';

输出为:

7[2629746]s

读取为7个单位,即2,629,746 s 。事实证明,2,629,746秒是民用日历中月份的平均长度。换句话说:

This reads as 7 units of 2,629,746s. It turns out that 2,629,746 seconds is the average length of the month in the civil calendar. Stated differently:

static_assert(months{1} == 2'629'746s);

(确切的数字除了赢得小额下注之外并不特别重要)

(the exact number is not particularly important except for winning bar bets)

month (单数)不是不是一个 chrono :: duration 。它是民用日历中一年中一个月的日历说明符。或者:

month (singular) on the other hand is not a chrono::duration. It is a calendrical specifier for a month of the year in the civil calendar. Or:

static_assert(month{7} == July);

可以用来形成这样的日期:

This can be used to form a date like this:

auto independence_day = month{7}/4d/2020y;

和<$ c的代数$ c> months 反映了这些不同的语义。例如,七月+七月是荒谬的,因此是编译时错误:

The algebra of month and months reflect these different semantics. For example "July + July" is nonsensical, and thus a compile-time error:

auto x = month{7} + month{7};
         ~~~~~~~~ ^ ~~~~~~~~
error: invalid operands to binary expression ('std::chrono::month' and 'std::chrono::month')

但这很合理:

auto constexpr x = month{7} + months{7};
static_assert(x == February);

然后:

auto constexpr x = months{7} + months{7};
static_assert(x == months{14});

然而:

auto b = February == months{14};
         ~~~~~~~~ ^  ~~~~~~~~~~
error: invalid operands to binary expression ('const std::chrono::month' and 'std::chrono::months')

month months 不仅不相等,甚至不可比。如果是水果类比的话,它们是苹果和橙子。 ;-)

I.e. month and months are not only not-equal, they are not even comparable. They are apples and oranges, if you're into fruit analogies. ;-)

。在之间。


如果为复数形式,则为 chrono :: duration






只有< chrono> 可以帮助类型安全您要确保这两个语义上不同但又相似的概念在代码中不会相互混淆。


And only <chrono> has the type safety to help you ensure that these two semantically distinct and yet similar concepts do not get confused with one another in your code.

这篇关于chrono :: month和chrono :: months有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆