xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/make_projected.h (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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___ALGORITHM_MAKE_PROJECTED_H
10 #define _LIBCPP___ALGORITHM_MAKE_PROJECTED_H
11 
12 #include <__concepts/same_as.h>
13 #include <__config>
14 #include <__functional/identity.h>
15 #include <__functional/invoke.h>
16 #include <__utility/forward.h>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 #if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 namespace ranges {
27 
28 template <class _Comp, class _Proj>
29 _LIBCPP_HIDE_FROM_ABI constexpr static
30 decltype(auto) __make_projected_comp(_Comp& __comp, _Proj& __proj) {
31   if constexpr (same_as<_Proj, identity>) {
32     // Avoid creating the lambda and just use the pristine comparator -- for certain algorithms, this would enable
33     // optimizations that rely on the type of the comparator.
34     return __comp;
35 
36   } else {
37     return [&](auto&& __lhs, auto&& __rhs) {
38       return std::invoke(__comp,
39                         std::invoke(__proj, std::forward<decltype(__lhs)>(__lhs)),
40                         std::invoke(__proj, std::forward<decltype(__rhs)>(__rhs)));
41     };
42   }
43 }
44 
45 } // namespace ranges
46 
47 _LIBCPP_END_NAMESPACE_STD
48 
49 #endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
50 
51 #endif // _LIBCPP___ALGORITHM_MAKE_PROJECTED_H
52