From 85ec267ea8f37c24b704d62c1e804e047482e57a Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Sat, 15 Jul 2023 19:37:05 +0100 Subject: [PATCH] Refactored etl::forward_like for C++11 compatibility --- include/etl/utility.h | 76 +++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/include/etl/utility.h b/include/etl/utility.h index 9b1e648d6..0c8fa240d 100644 --- a/include/etl/utility.h +++ b/include/etl/utility.h @@ -76,45 +76,57 @@ namespace etl /// If etl::remove_reference_t is const then returns a const reference if U is an lvalue, otherwise a const rvalue reference. /// If etl::remove_reference_t is not const then returns a reference if U is an lvalue, otherwise an rvalue reference. //****************************************************************************** + //*********************************** + /// T is const & lvalue. + //*********************************** template ETL_NODISCARD - ETL_CONSTEXPR14 - auto&& forward_like(U&& u) ETL_NOEXCEPT + ETL_CONSTEXPR + etl::enable_if_t>::value && etl::is_lvalue_reference::value, const etl::remove_reference_t&> + forward_like(U&& u) ETL_NOEXCEPT { - using TType = etl::remove_reference_t; - using UType = etl::remove_reference_t; + return static_cast&>(u); + } - if ETL_IF_CONSTEXPR (etl::is_const::value) - { - // For const TType - if ETL_IF_CONSTEXPR (etl::is_lvalue_reference::value) - { - // const lvalue - return static_cast(u); - } - else - { - // const rvalue - return static_cast(u); - } - } - else - { - // For non-const TType - if ETL_IF_CONSTEXPR (etl::is_lvalue_reference::value) - { - // lvalue - return static_cast(u); - } - else - { - // rvalue - return static_cast(u); - } - } + //*********************************** + /// T is const & rvalue. + //*********************************** + template + ETL_NODISCARD + ETL_CONSTEXPR + etl::enable_if_t>::value && !etl::is_lvalue_reference::value, const etl::remove_reference_t&&> + forward_like(U&& u) ETL_NOEXCEPT + { + return static_cast&&>(u); + } + + //*********************************** + /// T is not const & lvalue. + //*********************************** + template + ETL_NODISCARD + ETL_CONSTEXPR + etl::enable_if_t>::value && etl::is_lvalue_reference::value, etl::remove_reference_t&> + forward_like(U&& u) ETL_NOEXCEPT + { + return static_cast&>(u); + } + + //*********************************** + /// T is not const & rvalue. + //*********************************** + template + ETL_NODISCARD + ETL_CONSTEXPR + etl::enable_if_t>::value && !etl::is_lvalue_reference::value, etl::remove_reference_t&&> + forward_like(U&& u) ETL_NOEXCEPT + { + return static_cast&&>(u); } + //*********************************** // Defines the type that forward_like would cast to. + //*********************************** template using forward_like_t = decltype(etl::forward_like(etl::declval())); #endif