Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[libc++] add test for flat_set::insert not creating temporaries #138387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

huixie90
Copy link
Contributor

@huixie90 huixie90 commented May 3, 2025

fixes #119016

@huixie90 huixie90 requested a review from a team as a code owner May 3, 2025 08:08
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 3, 2025
@llvmbot
Copy link
Member

llvmbot commented May 3, 2025

@llvm/pr-subscribers-libcxx

Author: Hui (huixie90)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/138387.diff

3 Files Affected:

  • (added) libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp (+46)
  • (added) libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp (+46)
  • (added) libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h (+40)
diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp
new file mode 100644
index 0000000000000..97a9bc3bdbb3d
--- /dev/null
+++ b/libcxx/test/libcxx/containers/container.adaptors/flat.multiset/insert.temporary.pass.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// https://github.com/llvm/llvm-project/issues/119016
+
+#include <flat_set>
+
+#include <cassert>
+
+#include "../flat_helpers.h"
+#include "test_macros.h"
+
+bool test() {
+  using M = std::flat_multiset<TrackCopyMove>;
+  {
+    M m;
+    TrackCopyMove t;
+    m.insert(t);
+    assert(m.begin()->copy_count == 1);
+    assert(m.begin()->move_count == 0);
+  }
+  {
+    M m;
+    TrackCopyMove t;
+    m.emplace(t);
+    assert(m.begin()->copy_count == 1);
+    assert(m.begin()->move_count == 0);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
\ No newline at end of file
diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp
new file mode 100644
index 0000000000000..1642ec8bc89a1
--- /dev/null
+++ b/libcxx/test/libcxx/containers/container.adaptors/flat.set/insert.temporary.pass.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// <flat_set>
+
+// https://github.com/llvm/llvm-project/issues/119016
+
+#include <flat_set>
+
+#include <cassert>
+
+#include "../flat_helpers.h"
+#include "test_macros.h"
+
+bool test() {
+  using M = std::flat_set<TrackCopyMove>;
+  {
+    M m;
+    TrackCopyMove t;
+    m.insert(t);
+    assert(m.begin()->copy_count == 1);
+    assert(m.begin()->move_count == 0);
+  }
+  {
+    M m;
+    TrackCopyMove t;
+    m.emplace(t);
+    assert(m.begin()->copy_count == 1);
+    assert(m.begin()->move_count == 0);
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+
+  return 0;
+}
\ No newline at end of file
diff --git a/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h
new file mode 100644
index 0000000000000..6dbf55c659377
--- /dev/null
+++ b/libcxx/test/libcxx/containers/container.adaptors/flat_helpers.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
+#define TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
+
+struct TrackCopyMove {
+  mutable int copy_count = 0;
+  int move_count         = 0;
+
+  constexpr TrackCopyMove() = default;
+  constexpr TrackCopyMove(const TrackCopyMove& other) : copy_count(other.copy_count), move_count(other.move_count) {
+    ++copy_count;
+    ++other.copy_count;
+  }
+
+  constexpr TrackCopyMove(TrackCopyMove&& other) noexcept : copy_count(other.copy_count), move_count(other.move_count) {
+    ++move_count;
+    ++other.move_count;
+  }
+  constexpr TrackCopyMove& operator=(const TrackCopyMove& other) {
+    ++copy_count;
+    ++other.copy_count;
+    return *this;
+  }
+  constexpr TrackCopyMove& operator=(TrackCopyMove&& other) noexcept {
+    ++move_count;
+    ++other.move_count;
+    return *this;
+  }
+  constexpr bool operator==(const TrackCopyMove&) const { return true; }
+  constexpr bool operator<(const TrackCopyMove&) const { return false; }
+};
+
+#endif // TEST_LIBCXX_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] Should std::flat_set::insert construct a temporary value in its implementation?
3 participants