[libcxx-commits] [libcxx] [libc++][ABI BREAK] Make std::pair trivially copyable if its members are (PR #89652)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 22 18:44:20 PDT 2024


================
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// 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: c++20 || clang
+
+#include <type_traits>
+#include <utility>
+
+struct trivially_copyable {
+  int arr[4];
+};
+
+static_assert(std::is_trivially_copyable<std::pair<int, int>>::value, "");
+static_assert(std::is_trivially_copyable<std::pair<int, char>>::value, "");
+static_assert(std::is_trivially_copyable<std::pair<char, int>>::value, "");
+static_assert(std::is_trivially_copyable<std::pair<std::pair<char, char>, int>>::value, "");
+static_assert(std::is_trivially_copyable<std::pair<trivially_copyable, int>>::value, "");
+
+static_assert(!std::is_trivially_copyable<std::pair<int&, int>>::value, "");
----------------
frederick-vs-ja wrote:

It's possible to make `std::pair<int&, int>` trivially copyable and MSVC STL is already doing so ([demo](https://godbolt.org/z/fbjoEcb8G)).

MSVC STL's `pair` has a [deleted copy assignment operator taking `const volatile pair&`](https://github.com/microsoft/STL/blob/0515a05b394596de92d08cb0f352614479a2a883/stl/inc/utility#L314), and its usable assignment operators (e.g. [the one performing copy assignment](https://github.com/microsoft/STL/blob/0515a05b394596de92d08cb0f352614479a2a883/stl/inc/utility#L316-L320)) are templates. The usable assigment operators are non-trivial, but not counted for copy/move assignment operators, so the `pair` specialization can still be trivially copyable.

Perhaps we can adopt such technique when either `first_type` or `second_type` is a reference type.

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


More information about the libcxx-commits mailing list