xref: /aosp_15_r20/external/llvm-libc/src/__support/CPP/utility/forward.h (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- forward utility -----------------------------------------*- C++ -*-===//
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 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_FORWARD_H
9 #define LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_FORWARD_H
10 
11 #include "src/__support/CPP/type_traits/is_lvalue_reference.h"
12 #include "src/__support/CPP/type_traits/remove_reference.h"
13 #include "src/__support/macros/attributes.h"
14 #include "src/__support/macros/config.h"
15 
16 namespace LIBC_NAMESPACE_DECL {
17 namespace cpp {
18 
19 // forward
20 template <typename T>
forward(remove_reference_t<T> & value)21 LIBC_INLINE constexpr T &&forward(remove_reference_t<T> &value) {
22   return static_cast<T &&>(value);
23 }
24 
25 template <typename T>
forward(remove_reference_t<T> && value)26 LIBC_INLINE constexpr T &&forward(remove_reference_t<T> &&value) {
27   static_assert(!is_lvalue_reference_v<T>,
28                 "cannot forward an rvalue as an lvalue");
29   return static_cast<T &&>(value);
30 }
31 
32 } // namespace cpp
33 } // namespace LIBC_NAMESPACE_DECL
34 
35 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_UTILITY_FORWARD_H
36