From 2f7cab35207c693f0157d490c855e21258df66af Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 15 Apr 2025 07:54:22 +0100 Subject: [PATCH] C++11 compatibility updates --- include/etl/tuple.h | 87 +++++++++++++++++++++++++++------------------ test/test_tuple.cpp | 1 + 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/include/etl/tuple.h b/include/etl/tuple.h index 32b8686c..f1bdf319 100644 --- a/include/etl/tuple.h +++ b/include/etl/tuple.h @@ -76,8 +76,9 @@ namespace etl struct ignore_t { template - ETL_CONSTEXPR void operator =(T&&) const ETL_NOEXCEPT + ETL_CONSTEXPR ignore_t operator =(T&&) const ETL_NOEXCEPT { + return *this; } }; } @@ -158,36 +159,36 @@ namespace etl friend class tuple; template - ETL_CONSTEXPR14 - friend auto& get(tuple&); + ETL_CONSTEXPR14 + friend etl::tuple_element_t>& get(tuple&); template ETL_CONSTEXPR14 - friend auto&& get(tuple&&); + friend etl::tuple_element_t>&& get(tuple&&); template ETL_CONSTEXPR14 - friend const auto& get(const tuple&); + friend const etl::tuple_element_t>& get(const tuple&); template ETL_CONSTEXPR14 - friend const auto&& get(const tuple&&); + friend const etl::tuple_element_t>&& get(const tuple&&); template - ETL_CONSTEXPR14 - friend auto& get(tuple&); + ETL_CONSTEXPR14 + friend T& get(tuple&); template - ETL_CONSTEXPR14 - friend auto&& get(tuple&&); + ETL_CONSTEXPR14 + friend T&& get(tuple&&); template - ETL_CONSTEXPR14 - friend const auto& get(const tuple&); + ETL_CONSTEXPR14 + friend const T& get(const tuple&); template - ETL_CONSTEXPR14 - friend const auto&& get(const tuple&&); + ETL_CONSTEXPR14 + friend const T&& get(const tuple&&); //********************************* /// Types @@ -728,7 +729,7 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto& get(tuple& t) + etl::tuple_element_t>& get(tuple& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); @@ -747,7 +748,7 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - const auto& get(const tuple& t) + const etl::tuple_element_t>& get(const tuple& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); @@ -766,7 +767,7 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto&& get(tuple&& t) + etl::tuple_element_t>&& get(tuple&& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); @@ -774,7 +775,7 @@ namespace etl using tuple_type = etl::nth_base_t>&&; // Cast the tuple to the selected type and get the value. - return static_cast(t).get_value(); + return etl::forward>>(static_cast(t).get_value()); } //*************************************************************************** @@ -785,15 +786,15 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - const auto&& get(const tuple&& t) + const etl::tuple_element_t>&& get(const tuple&& t) { ETL_STATIC_ASSERT(Index < sizeof...(TTypes), "etl::get - Index out of range"); // Get the type at this index. - using tuple_type = const etl::nth_base_t>&&; + using tuple_type = const etl::nth_base_t>&&; // Cast the tuple to the selected type and get the value. - return static_cast(t).get_value(); + return etl::forward>>(static_cast(t).get_value()); } //*************************************************************************** @@ -804,10 +805,10 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto& get(tuple& t) + T& get(tuple& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); - ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); + ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>; @@ -824,10 +825,10 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - const auto& get(const tuple& t) + const T& get(const tuple& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); - ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); + ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>; @@ -844,16 +845,16 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto&& get(tuple&& t) + T&& get(tuple&& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); - ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); + ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>; // Cast the tuple to the selected type and get the value. - return static_cast(t).get_value(); + return etl::forward(static_cast(t).get_value()); } //*************************************************************************** @@ -864,16 +865,16 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - const auto&& get(const tuple&& t) + const T&& get(const tuple&& t) { ETL_STATIC_ASSERT(!(etl::has_duplicates_of::value), "etl::get - Tuple contains duplicate instances of T"); - ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); + ETL_STATIC_ASSERT((etl::is_one_of::value), "etl::get - Tuple does not contain the specified type"); // Get the tuple base type that contains a T using tuple_type = etl::private_tuple::tuple_type_base_t>; // Cast the tuple to the selected type and get the value. - return static_cast(t).get_value(); + return etl::forward(static_cast(t).get_value()); } #if ETL_USING_CPP17 @@ -886,7 +887,8 @@ namespace etl /// Creates a tuple of references to the provided arguments. //*************************************************************************** template - ETL_CONSTEXPR etl::tuple tie(TTypes&... args) + ETL_CONSTEXPR + etl::tuple tie(TTypes&... args) { return { args... }; } @@ -910,6 +912,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto select_from_tuple(TTuple&& tuple, etl::index_sequence) + -> etl::tuple>...> { ETL_STATIC_ASSERT(sizeof...(Indices) <= etl::tuple_size>::value, "Number of indices is greater than the tuple size"); @@ -924,6 +927,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto select_from_tuple(TTuple&& tuple) + -> etl::tuple>...> { return select_from_tuple(etl::forward(tuple), etl::index_sequence{}); } @@ -947,6 +951,7 @@ namespace etl template ETL_CONSTEXPR14 auto tuple_cat_impl(Tuple1&& t1, etl::index_sequence, Tuple2&& t2, etl::index_sequence) + -> etl::tuple>..., etl::tuple_element_t>...> { return etl::tuple>..., etl::tuple_element_t>...> @@ -960,7 +965,7 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto tuple_cat(Tuple&& t) + auto tuple_cat(Tuple&& t) -> Tuple { return etl::forward(t); } @@ -972,6 +977,10 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto tuple_cat(Tuple1&& t1, Tuple2&& t2, Tuples&&... ts) + -> decltype(private_tuple::tuple_cat_impl(etl::forward(t1), + etl::make_index_sequence>::value>{}, + etl::forward(t2), + etl::make_index_sequence>::value>{})) { auto concatenated = private_tuple::tuple_cat_impl(etl::forward(t1), etl::make_index_sequence>::value>{}, @@ -993,6 +1002,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_std_impl(const TEtl_Tuple& etl_tuple, etl::index_sequence) + -> std::tuple::type...> { return std::tuple::type...>(etl::get(etl_tuple)...); } @@ -1002,6 +1012,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_std_impl(TEtl_Tuple&& etl_tuple, etl::index_sequence) + -> std::tuple::type...> { return std::tuple::type...>(etl::move(etl::get(etl_tuple))...); } @@ -1014,6 +1025,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_std(const etl::tuple& etl_tuple) + -> std::tuple::type...> { return private_tuple::to_std_impl(etl_tuple, etl::make_index_sequence_for()); } @@ -1025,6 +1037,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_std(etl::tuple&& etl_tuple) + -> std::tuple::type...> { return private_tuple::to_std_impl(etl::move(etl_tuple), etl::make_index_sequence_for()); } @@ -1040,6 +1053,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl_impl(const TStd_Tuple& std_tuple, etl::index_sequence) + -> etl::tuple::type...> { return etl::tuple::type...>(std::get(std_tuple)...); } @@ -1048,7 +1062,8 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - auto to_etl_impl(TStd_Tuple&& std_tuple, etl::index_sequence) + auto to_etl_impl(TStd_Tuple&& std_tuple, etl::index_sequence) + -> etl::tuple::type...> { return etl::tuple::type...>(std::move(std::get(std_tuple))...); } @@ -1061,6 +1076,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl(const std::tuple& std_tuple) + -> etl::tuple::type...> { return private_tuple::to_etl_impl(std_tuple, etl::make_index_sequence_for()); } @@ -1072,6 +1088,7 @@ namespace etl ETL_NODISCARD ETL_CONSTEXPR14 auto to_etl(std::tuple&& std_tuple) + -> etl::tuple::type...> { return private_tuple::to_etl_impl(etl::move(std_tuple), etl::make_index_sequence_for()); } @@ -1086,7 +1103,7 @@ namespace etl template ETL_NODISCARD ETL_CONSTEXPR14 - bool tuple_equality(const TTuple1& /*lhs*/, const TTuple2& /*rhs*/, etl::index_sequence<>) + bool tuple_equality(const TTuple1& /*lhs*/, const TTuple2& /*rhs*/, etl::index_sequence<>) { return true; } diff --git a/test/test_tuple.cpp b/test/test_tuple.cpp index ed3ff2cd..d1a003a5 100644 --- a/test/test_tuple.cpp +++ b/test/test_tuple.cpp @@ -44,6 +44,7 @@ namespace template auto to_array(etl::index_sequence) + -> std::array { return std::array{Indices...}; }