[libcxx-commits] [libcxx] [libc++] Optimize vector growing of trivially relocatable types (PR #76657)

Amirreza Ashouri via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 11 16:30:09 PST 2024


================
@@ -273,6 +274,9 @@ class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS unique_ptr {
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 void swap(unique_ptr& __u) _NOEXCEPT { __ptr_.swap(__u.__ptr_); }
 };
 
+template <class _Tp>
+struct __libcpp_is_trivially_relocatable<unique_ptr<_Tp>> : true_type {};
+
----------------
AMP999 wrote:

Another way to achieve this is to use a member typedef like BSL does:
```c++
template<class _Tp, class _Dp>
class unique_ptr {
public:
  using __libcpp_trivially_relocatable_tag = conditional_t<
    __libcpp_is_trivially_relocatable<_Dp>::value && __libcpp_is_trivially_relocatable<typename _Dp::pointer>::value,
    unique_ptr, void>;
};
```
and then instead of specializing `__libcpp_is_trivially_relocatable` you could simply make the primary template look like this:
```c++
template <class _Tp, class _Enable = void>
struct __libcpp_is_trivially_relocatable : is_trivially_copyable<_Tp> {};

template <class _Tp>
struct __libcpp_is_trivially_relocatable<_Tp, __enable_if_t<is_same_v<typename _Tp::__libcpp_trivially_relocatable_tag, _Tp>>> : true_type {};
```
Then you wouldn't need to add specializations of `__libcpp_is_trivially_relocatable` in any other headers.

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


More information about the libcxx-commits mailing list