64 lines · c
1// -*- C++ -*-2//===----------------------------------------------------------------------===//3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___UTILITY_FORWARD_LIKE_H11#define _LIBCPP___UTILITY_FORWARD_LIKE_H12 13#include <__config>14#include <__type_traits/conditional.h>15#include <__type_traits/is_base_of.h>16#include <__type_traits/is_const.h>17#include <__type_traits/is_reference.h>18#include <__type_traits/remove_reference.h>19 20#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif23 24_LIBCPP_BEGIN_NAMESPACE_STD25 26#if _LIBCPP_STD_VER >= 2327 28template <class _Ap, class _Bp>29using _CopyConst _LIBCPP_NODEBUG = _If<is_const_v<_Ap>, const _Bp, _Bp>;30 31template <class _Ap, class _Bp>32using _OverrideRef _LIBCPP_NODEBUG = _If<is_rvalue_reference_v<_Ap>, remove_reference_t<_Bp>&&, _Bp&>;33 34template <class _Ap, class _Bp>35using _ForwardLike _LIBCPP_NODEBUG = _OverrideRef<_Ap&&, _CopyConst<remove_reference_t<_Ap>, remove_reference_t<_Bp>>>;36 37template <class _Tp, class _Up>38[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto39forward_like(_LIBCPP_LIFETIMEBOUND _Up&& __ux) noexcept -> _ForwardLike<_Tp, _Up> {40 return static_cast<_ForwardLike<_Tp, _Up>>(__ux);41}42 43// This function is used for `deducing this` cases where you want to make sure the operation is performed on the class44// itself and not on a derived class. For example45// struct S {46// template <class Self>47// void func(Self&& self) {48// // This will always call `do_something` of S instead of any class derived from S.49// std::__forward_as<Self, S>(self).do_something();50// }51// };52template <class _Tp, class _As, class _Up>53[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _ForwardLike<_Tp, _As>54__forward_as(_LIBCPP_LIFETIMEBOUND _Up&& __val) noexcept {55 static_assert(is_base_of_v<_As, remove_reference_t<_Up>>);56 return static_cast<_ForwardLike<_Tp, _As>>(__val);57}58 59#endif // _LIBCPP_STD_VER >= 2360 61_LIBCPP_END_NAMESPACE_STD62 63#endif // _LIBCPP___UTILITY_FORWARD_LIKE_H64