xref: /freebsd/contrib/llvm-project/libcxx/include/__type_traits/is_replaceable.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H
10 #define _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H
11 
12 #include <__config>
13 #include <__type_traits/enable_if.h>
14 #include <__type_traits/integral_constant.h>
15 #include <__type_traits/is_same.h>
16 #include <__type_traits/is_trivially_copyable.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 // A type is replaceable if, with `x` and `y` being different objects, `x = std::move(y)` is equivalent to:
25 //
26 //  std::destroy_at(&x)
27 //  std::construct_at(&x, std::move(y))
28 //
29 // This allows turning a move-assignment into a sequence of destroy + move-construct, which
30 // is often more efficient. This is especially relevant when the move-construct is in fact
31 // part of a trivial relocation from somewhere else, in which case there is a huge win.
32 //
33 // Note that this requires language support in order to be really effective, but we
34 // currently emulate the base template with something very conservative.
35 template <class _Tp, class = void>
36 struct __is_replaceable : is_trivially_copyable<_Tp> {};
37 
38 template <class _Tp>
39 struct __is_replaceable<_Tp, __enable_if_t<is_same<_Tp, typename _Tp::__replaceable>::value> > : true_type {};
40 
41 template <class _Tp>
42 inline const bool __is_replaceable_v = __is_replaceable<_Tp>::value;
43 
44 // Determines whether an allocator member of a container is replaceable.
45 //
46 // First, we require the allocator type to be considered replaceable. If not, then something fishy might be
47 // happening. Assuming the allocator type is replaceable, we conclude replaceability of the allocator as a
48 // member of the container if the allocator always compares equal (in which case propagation doesn't matter),
49 // or if the allocator always propagates on assignment, which is required in order for move construction and
50 // assignment to be equivalent.
51 template <class _AllocatorTraits>
52 struct __container_allocator_is_replaceable
53     : integral_constant<bool,
54                         __is_replaceable_v<typename _AllocatorTraits::allocator_type> &&
55                             (_AllocatorTraits::is_always_equal::value ||
56                              (_AllocatorTraits::propagate_on_container_move_assignment::value &&
57                               _AllocatorTraits::propagate_on_container_copy_assignment::value))> {};
58 
59 _LIBCPP_END_NAMESPACE_STD
60 
61 #endif // _LIBCPP___TYPE_TRAITS_IS_REPLACEABLE_H
62