[libcxx-commits] [libcxx] [libc++][flat_map] Applied `[[nodiscard]]` (PR #169453)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 25 03:37:31 PST 2025


================
@@ -6,15 +6,107 @@
 //
 //===----------------------------------------------------------------------===//
 
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+// REQUIRES: std-at-least-c++23
 
 // <flat_map>
 
 // [[nodiscard]] bool empty() const noexcept;
 
 #include <flat_map>
+#include <utility>
 
-void f() {
-  std::flat_map<int, int> c;
-  c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+template <typename T>
+struct TransparentKey {
+  T t;
+
+  constexpr explicit operator T() const { return t; }
+};
+
+struct TransparentCompare {
+  using is_transparent = void; // This makes the comparator transparent
+
+  template <typename T>
+  constexpr bool operator()(const T& t, const TransparentKey<T>& transparent) const {
+    return t < transparent.t;
+  }
+
+  template <typename T>
+  constexpr bool operator()(const TransparentKey<T>& transparent, const T& t) const {
+    return transparent.t < t;
+  }
+
+  template <typename T>
+  constexpr bool operator()(const T& t1, const T& t2) const {
+    return t1 < t2;
+  }
+};
+
+void test() {
+  std::flat_map<int, int, TransparentCompare> fm;
+  const std::flat_map<int, int, TransparentCompare> cfm{};
+
+  fm.begin();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.begin();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fm.end();      // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.end();     // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fm.rbegin();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.rbegin();  // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fm.rend();     // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.rend();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.begin();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.cend();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.crbegin(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  cfm.crend();   // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  fm.empty();    // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fm.size();     // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+  fm.max_size(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+  fm.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
----------------
philnik777 wrote:

You have `empty()` twice.

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


More information about the libcxx-commits mailing list