[libcxx-commits] [libcxx] [libc++] Implement P0493R5: Atomic minimum/maximum (PR #180333)

Damien L-G via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 9 04:33:21 PST 2026


================
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++26
+// XFAIL: !has-64-bit-atomics
+
+// integral-type fetch_min(integral-type, memory_order = memory_order::seq_cst) const noexcept;
+
+#include <atomic>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+#include <utility>
+
+#include "atomic_helpers.h"
+#include "test_macros.h"
+
+template <typename T>
+concept has_fetch_min = requires {
+  std::declval<T const>().fetch_min(std::declval<T>());
+  std::declval<T const>().fetch_min(std::declval<T>(), std::declval<std::memory_order>());
+};
+
+template <typename T>
+struct TestDoesNotHaveFetchMin {
+  void operator()() const { static_assert(!has_fetch_min<std::atomic_ref<T>>); }
+};
+
+template <typename T>
+struct TestFetchMin {
+  void operator()() const {
+    static_assert(std::is_integral_v<T>);
+
+    alignas(std::atomic_ref<T>::required_alignment) T x(T(3));
+    std::atomic_ref<T> const a(x);
+
+    {
+      std::same_as<T> decltype(auto) y = a.fetch_min(T(2));
+      assert(y == T(3));
+      assert(x == T(2));
----------------
dalg24 wrote:

```suggestion
      assert(x == T(2));
      y = a.fetch_min(T(4));
      assert(y == T(2));
      assert(x == T(2));
```
Please add a check where the referenced value is not expected to change.
(same comment below with a memory order argument, and for fetch_max)

https://github.com/llvm/llvm-project/pull/180333


More information about the libcxx-commits mailing list