瀏覽代碼

Fix a bug in std::chrono::abs where it would fail when the duration's period had not been reduced.s

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@367120 91177308-0d34-0410-b5e6-96231b3b80d8
Marshall Clow 6 年之前
父節點
當前提交
4bd3f7d588
共有 2 個文件被更改,包括 7 次插入1 次删除
  1. 1 1
      include/chrono
  2. 6 0
      test/std/utilities/time/time.duration/time.duration.alg/abs.pass.cpp

+ 1 - 1
include/chrono

@@ -1428,7 +1428,7 @@ typename enable_if
 >::type
 >::type
 abs(duration<_Rep, _Period> __d)
 abs(duration<_Rep, _Period> __d)
 {
 {
-    return __d >= __d.zero() ? __d : -__d;
+    return __d >= __d.zero() ? +__d : -__d;
 }
 }
 #endif
 #endif
 
 

+ 6 - 0
test/std/utilities/time/time.duration/time.duration.alg/abs.pass.cpp

@@ -49,5 +49,11 @@ int main(int, char**)
     static_assert(h2.count() == 3, "");
     static_assert(h2.count() == 3, "");
     }
     }
 
 
+    {
+//  Make sure it works for durations that are not LCD'ed - example from LWG3091
+    constexpr auto d = std::chrono::abs(std::chrono::duration<int, std::ratio<60, 100>>{2});
+    static_assert(d.count() == 2, "");
+    }
+
   return 0;
   return 0;
 }
 }