Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
forward_like.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5///
6/// @file forward_like.hpp
7/// @brief Implementation of a backport of @c std::forward_like
8///
9#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_FORWARD_LIKE_HPP_
10#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_FORWARD_LIKE_HPP_
11
12// IWYU pragma: private, include "arene/base/utility.hpp"
13// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/compiler_support/attributes.hpp"
17#include "arene/base/stdlib_choice/is_const.hpp"
18#include "arene/base/stdlib_choice/is_lvalue_reference.hpp"
19#include "arene/base/stdlib_choice/remove_reference.hpp"
20#include "arene/base/type_traits/conditional.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23namespace arene {
24namespace base {
25
26namespace forward_like_detail {
27
28/// @brief implementation helper for @c arene::base::forward_like
29/// @see arene::base::forward_like
30template <typename From>
31class forward_like {
32 /// @brief Trait which deduces the return type based on From
33 /// @tparam To the type which the qualifiers from From are applied to.
34 /// @tparam Referenced the referenced type, defaulted
35 /// @return A type which will be a reference of base type @c To with the following properties:
36 /// * If @c std::remove_reference_t<From> is a const-qualified type, then the
37 /// referenced type of the return type is <c> std::remove_reference_t<To> const </c>.
38 /// Otherwise, the referenced type is @c std::remove_reference_t<To> const-qualified.
39 /// * If @c From is an lvalue reference, then the return type is also an
40 /// lvalue reference, otherwise it is an rvalue reference.
41 template <
42 typename To,
43 typename Referenced = conditional_t<
44 std::is_const<std::remove_reference_t<From>>::value,
45 std::remove_reference_t<To> const,
46 std::remove_reference_t<To>>>
47 using forward_like_result_t = conditional_t< //
48 std::is_lvalue_reference<From&&>::value,
49 Referenced&,
50 Referenced&&>;
51
52 public:
53 // parasoft-begin-suppress AUTOSAR-A8_4_6-a-2 "False positive: Value is forwarded as appropriate"
54 // parasoft-begin-suppress AUTOSAR-A8_4_5-a-2 "False positive: Value is forwarded as appropriate"
55 // parasoft-begin-suppress AUTOSAR-A12_8_4-a-2 "False positive: Value is forwarded as appropriate"
56 /// @brief applies forward_like_result_t to the input value reference.
57 /// @tparam From The type to take the reference and qualification properties from.
58 /// @tparam To The type to apply the reference and qualification properties to.
59 /// @param val The value to apply the reference and qualification properties to.
60 /// @return A reference of base type @c To with the following properties:
61 /// * If @c std::remove_reference_t<From> is a const-qualified type, then the
62 /// referenced type of the return type is <c> std::remove_reference_t<To> const </c>.
63 /// Otherwise, the referenced type is @c std::remove_reference_t<To> const-qualified.
64 /// * If @c From is an lvalue reference, then the return type is also an
65 /// lvalue reference, otherwise it is an rvalue reference.
66 /// @see arene::base::forward_like
67 template <typename To>
68 ARENE_NODISCARD constexpr auto operator()(To&& val) const noexcept -> forward_like_result_t<To> {
69 return static_cast<forward_like_result_t<To>>(val);
70 }
71 // parasoft-end-suppress AUTOSAR-A8_4_6-a-2
72 // parasoft-end-suppress AUTOSAR-A8_4_5-a-2
73 // parasoft-end-suppress AUTOSAR-A12_8_4-a-2
74};
75
76} // namespace forward_like_detail
77
78///
79/// @brief A backport of C++23's [@c std::forward_like](https://en.cppreference.com/w/cpp/utility/forward_like) , it
80/// allows forwarding a type with the same reference and const qualifications as another type.
81///
82/// @tparam From The type to take the reference and qualification properties from.
83/// @tparam To The type to apply the reference and qualification properties to.
84/// @param val The value to apply the reference and qualification properties to.
85/// @return A reference of base type @c To with the following properties:
86/// * If @c std::remove_reference_t<From> is a const-qualified type, then the
87/// referenced type of the return type is <c> std::remove_reference_t<To> const </c>.
88/// Otherwise, the referenced type is @c std::remove_reference_t<To> const-qualified.
89/// * If @c From is an lvalue reference, then the return type is also an
90/// lvalue reference, otherwise it is an rvalue reference.
91///
92/// @note Mandates <br>
93/// * @c From is a referenceable type
94template <typename From>
95extern constexpr auto forward_like = forward_like_detail::forward_like<From>{};
96
97} // namespace base
98} // namespace arene
99
100#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_UTILITY_FORWARD_LIKE_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr auto forward_like
A backport of C++23's std::forward_like @a , it allows forwarding a type with the same reference and ...
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10