tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
forward_like.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2024 Tobias Hienzsch
3
4#ifndef TETL_UTILITY_FORWARD_LIKE_HPP
5#define TETL_UTILITY_FORWARD_LIKE_HPP
6
7#include <etl/_type_traits/is_const.hpp>
8#include <etl/_type_traits/is_lvalue_reference.hpp>
9#include <etl/_type_traits/remove_reference.hpp>
10#include <etl/_utility/as_const.hpp>
11#include <etl/_utility/move.hpp>
12
13namespace etl {
14
15template <typename T, typename U>
16[[nodiscard]] constexpr auto forward_like(U&& x) noexcept -> auto&&
17{
18 constexpr auto isAddingConst = etl::is_const_v<etl::remove_reference_t<T>>;
19
20 if constexpr (etl::is_lvalue_reference_v<T&&>) {
21 if constexpr (isAddingConst) {
22 return etl::as_const(x);
23 } else {
24 return static_cast<U&>(x);
25 }
26 } else {
27 if constexpr (isAddingConst) {
28 return etl::move(etl::as_const(x));
29 } else {
30 return etl::move(x);
31 }
32 }
33}
34
35} // namespace etl
36
37#endif // TETL_UTILITY_FORWARD_LIKE_HPP
Definition adjacent_find.hpp:9
constexpr auto forward_like(U &&x) noexcept -> auto &&
Definition forward_like.hpp:16