[libcxx-commits] [libcxx] [libc++] Avoid double comparisons when uniquing flat_set/flat_map (PR #214118)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 4 20:10:13 PDT 2026


================
@@ -35,6 +35,25 @@ struct DefaultCtableComp {
   bool default_constructed_ = false;
 };
 
+struct CountingComp {
+  constexpr explicit CountingComp(int& count) : count_(count) {}
+
+  constexpr bool operator()(int x, int y) const {
+    ++count_;
+    return x < y;
+  }
+
+  int& count_;
+};
+
+void test_dedup_uses_one_direction_comparisons() {
+  int count = 0;
+  std::flat_map<int, int, CountingComp> map({{0, 1}, {0, 2}}, CountingComp(count));
+  assert(map.size() == 1);
+  assert(map.begin()->first == 0);
+  assert(count == 2);
----------------
frederick-vs-ja wrote:

This should be libc++-specific. Also, perhaps we should run this function in constant evaluation since C++26.

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


More information about the libcxx-commits mailing list