non conforming return value for std::chrono::duration::operator%() in Microsoft C++ 2012

时光怂恿深爱的人放手 提交于 2019-12-06 07:41:38
Howard Hinnant

Yes, this is a bug and the fix is available in Visual Studio 2015.

The reason it's an implementation bug comes from dimensional analysis.

Clearly if we subtract seconds from seconds the result is seconds.

seconds = seconds - seconds

And if we divide seconds by seconds, the result is a scalar (a scalar has no units).

scalar = seconds / seconds

And finally one can multiply seconds by a scalar and get seconds.

seconds = seconds * scalar
seconds = scalar * seconds

In [expr.mul]/p4 the standard defines the modulus operator:

... if the quotient a/b is representable in the type of the result, (a/b)*b + a%b is equal to a ...

Said slightly differently:

a % b = a - (a/b)*b

So a duration % duration has the same units as:

seconds - (seconds/seconds)*seconds

which simplifies down to just seconds, and not a scalar.

The same analysis explains why:

seconds % scalar = seconds
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!