Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:06

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___UTILITY_FORWARD_LIKE_H
0011 #define _LIBCPP___UTILITY_FORWARD_LIKE_H
0012 
0013 #include <__config>
0014 #include <__type_traits/conditional.h>
0015 #include <__type_traits/is_base_of.h>
0016 #include <__type_traits/is_const.h>
0017 #include <__type_traits/is_reference.h>
0018 #include <__type_traits/remove_reference.h>
0019 
0020 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0021 #  pragma GCC system_header
0022 #endif
0023 
0024 _LIBCPP_BEGIN_NAMESPACE_STD
0025 
0026 #if _LIBCPP_STD_VER >= 23
0027 
0028 template <class _Ap, class _Bp>
0029 using _CopyConst _LIBCPP_NODEBUG = _If<is_const_v<_Ap>, const _Bp, _Bp>;
0030 
0031 template <class _Ap, class _Bp>
0032 using _OverrideRef _LIBCPP_NODEBUG = _If<is_rvalue_reference_v<_Ap>, remove_reference_t<_Bp>&&, _Bp&>;
0033 
0034 template <class _Ap, class _Bp>
0035 using _ForwardLike _LIBCPP_NODEBUG = _OverrideRef<_Ap&&, _CopyConst<remove_reference_t<_Ap>, remove_reference_t<_Bp>>>;
0036 
0037 template <class _Tp, class _Up>
0038 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto
0039 forward_like(_LIBCPP_LIFETIMEBOUND _Up&& __ux) noexcept -> _ForwardLike<_Tp, _Up> {
0040   return static_cast<_ForwardLike<_Tp, _Up>>(__ux);
0041 }
0042 
0043 // This function is used for `deducing this` cases where you want to make sure the operation is performed on the class
0044 // itself and not on a derived class. For example
0045 //   struct S {
0046 //     template <class Self>
0047 //     void func(Self&& self) {
0048 //       // This will always call `do_something` of S instead of any class derived from S.
0049 //       std::__forward_as<Self, S>(self).do_something();
0050 //     }
0051 //   };
0052 template <class _Tp, class _As, class _Up>
0053 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _ForwardLike<_Tp, _As>
0054 __forward_as(_LIBCPP_LIFETIMEBOUND _Up&& __val) noexcept {
0055   static_assert(is_base_of_v<_As, remove_reference_t<_Up>>);
0056   return static_cast<_ForwardLike<_Tp, _As>>(__val);
0057 }
0058 
0059 #endif // _LIBCPP_STD_VER >= 23
0060 
0061 _LIBCPP_END_NAMESPACE_STD
0062 
0063 #endif // _LIBCPP___UTILITY_FORWARD_LIKE_H