[libcxx-commits] [PATCH] D129442: [libc++][chrono] Avoid tautological comparisions.

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Jul 10 05:40:44 PDT 2022


Mordante created this revision.
Mordante added reviewers: ldionne, philnik.
Herald added a project: All.
Mordante requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

In our implementation the year is always less than or equal to the
class' `max()`. It's unlikely this ever changes since changing the
year's range will be an ABI break. A static_assert is added as a
guard.

This was reported by @philnik.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129442

Files:
  libcxx/include/__chrono/year.h


Index: libcxx/include/__chrono/year.h
===================================================================
--- libcxx/include/__chrono/year.h
+++ libcxx/include/__chrono/year.h
@@ -12,6 +12,7 @@
 
 #include <__chrono/duration.h>
 #include <__config>
+#include <limits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -100,9 +101,11 @@
 year& year::operator-=(const years& __dy) noexcept
 { *this = *this - __dy; return *this; }
 
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool year::ok() const noexcept
-{ return static_cast<int>(min()) <= __y && __y <= static_cast<int>(max()); }
+_LIBCPP_HIDE_FROM_ABI constexpr bool year::ok() const noexcept {
+  static_assert(static_cast<int>(std::numeric_limits<decltype(__y)>::max()) == static_cast<int>(max()));
+  return static_cast<int>(min()) <= __y;
+}
+
 } // namespace chrono
 
 _LIBCPP_END_NAMESPACE_STD


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129442.443496.patch
Type: text/x-patch
Size: 887 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220710/98fde2a7/attachment.bin>


More information about the libcxx-commits mailing list