From 7fdea7f7ae970380ab5541b973a8aaea37297676 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 25 Mar 2026 20:28:12 +0100 Subject: [PATCH] Adjustments in tuple.h from review (#1350) * Print test names at test time (#1343) * Adjustments in tuple.h from review --------- Co-authored-by: John Wellbelove --- include/etl/tuple.h | 86 ++++++++++++++++++++-------------------- test/test_tuple.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 136 insertions(+), 45 deletions(-) diff --git a/include/etl/tuple.h b/include/etl/tuple.h index dde08d13..143ee12e 100644 --- a/include/etl/tuple.h +++ b/include/etl/tuple.h @@ -276,7 +276,7 @@ namespace etl /// Implicit conversion //********************************* template () == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(tuple& other) @@ -290,7 +290,7 @@ namespace etl /// Explicit conversion //********************************* template () == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(tuple& other) @@ -304,7 +304,7 @@ namespace etl /// Implicit conversion //********************************* template () == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(const tuple& other) @@ -318,7 +318,7 @@ namespace etl /// Explicit conversion //********************************* template () == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && !etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 explicit tuple(const tuple& other) @@ -332,7 +332,7 @@ namespace etl /// Implicit conversion //********************************* template () == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(tuple&& other) @@ -399,7 +399,7 @@ namespace etl //********************************* template >::value && (number_of_types() == number_of_types()) && - (number_of_types() >= 1U) && + (number_of_types() >= 1U) && etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(UHead&& head, UTail&&... tail) ETL_NOEXCEPT @@ -428,8 +428,8 @@ namespace etl /// Implicit conversion. //********************************* template () == 2U && - etl ::is_convertible::value && - etl ::is_convertible::value, int> = 0> + etl::is_convertible::value && + etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) @@ -442,8 +442,8 @@ namespace etl /// Explicit conversion. //********************************* template () == 2U && - (!etl ::is_convertible::value || - !etl ::is_convertible::value), int> = 0> + (!etl::is_convertible::value || + !etl::is_convertible::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) @@ -456,8 +456,8 @@ namespace etl /// Implicit conversion. //********************************* template () == 2U && - etl ::is_convertible::value && - etl ::is_convertible::value, int> = 0> + etl::is_convertible::value && + etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(const ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) @@ -470,8 +470,8 @@ namespace etl /// Explicit conversion. //********************************* template () == 2U && - (!etl ::is_convertible::value || - !etl ::is_convertible::value), int> = 0> + (!etl::is_convertible::value || + !etl::is_convertible::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(const ETL_OR_STD::pair& p) ETL_NOEXCEPT : base_type(p.second) @@ -484,8 +484,8 @@ namespace etl /// Implicit conversion. //********************************* template () == 2U && - etl ::is_convertible::value && - etl ::is_convertible::value, int> = 0> + etl::is_convertible::value && + etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) @@ -498,8 +498,8 @@ namespace etl /// Explicit conversion. //********************************* template () == 2U && - (!etl ::is_convertible::value || - !etl ::is_convertible::value), int> = 0> + (!etl::is_convertible::value || + !etl::is_convertible::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(ETL_OR_STD::pair&& p) ETL_NOEXCEPT : base_type(etl::forward(p.second)) @@ -512,12 +512,12 @@ namespace etl /// Implicit conversion. //********************************* template () == 2U && - etl ::is_convertible::value && - etl ::is_convertible::value, int> = 0> + etl::is_convertible::value && + etl::is_convertible::value, int> = 0> ETL_CONSTEXPR14 tuple(const ETL_OR_STD::pair&& p) ETL_NOEXCEPT - : base_type(etl::forward(p.second)) - , value(etl::forward(p.first)) + : base_type(etl::forward(p.second)) + , value(etl::forward(p.first)) { } @@ -526,12 +526,12 @@ namespace etl /// Explicit conversion. //********************************* template () == 2U && - (!etl ::is_convertible::value || - !etl ::is_convertible::value), int> = 0> + (!etl::is_convertible::value || + !etl::is_convertible::value), int> = 0> ETL_CONSTEXPR14 explicit tuple(const ETL_OR_STD::pair&& p) ETL_NOEXCEPT - : base_type(p.second) - , value(p.first) + : base_type(etl::forward(p.second)) + , value(etl::forward(p.first)) { } @@ -605,8 +605,8 @@ namespace etl ETL_CONSTEXPR14 tuple& operator =(const ETL_OR_STD::pair&& p) { - get_value() = p.first; - get_base().get_value() = p.second; + get_value() = etl::forward(p.first); + get_base().get_value() = etl::forward(p.second); return *this; } @@ -763,7 +763,7 @@ namespace etl //*************************************************************************** /// Extracts the element at Index from the tuple. - /// Index must be an integer value in sizeof...(TTypes)). + /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a reference. //*************************************************************************** template @@ -782,7 +782,7 @@ namespace etl //*************************************************************************** /// Extracts the element at Index from the tuple. - /// Index must be an integer value in [?0?, sizeof...(TTypes)). + /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a const reference. //*************************************************************************** template @@ -801,7 +801,7 @@ namespace etl //*************************************************************************** /// Extracts the element at Index from the tuple. - /// Index must be an integer value in [?0?, sizeof...(TTypes)). + /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns an rvalue reference. //*************************************************************************** template @@ -820,7 +820,7 @@ namespace etl //*************************************************************************** /// Extracts the element at Index from the tuple. - /// Index must be an integer value in [?0?, sizeof...(TTypes)). + /// Index must be an integer value in [0, sizeof...(TTypes)). /// Returns a const rvalue reference. //*************************************************************************** template @@ -1017,17 +1017,17 @@ 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>{})) + -> decltype(tuple_cat(private_tuple::tuple_cat_impl(etl::forward(t1), + etl::make_index_sequence>::value>{}, + etl::forward(t2), + etl::make_index_sequence>::value>{}), + etl::forward(ts)...)) { - auto concatenated = private_tuple::tuple_cat_impl(etl::forward(t1), - etl::make_index_sequence>::value>{}, - etl::forward(t2), - etl::make_index_sequence>::value>{}); - - return tuple_cat(etl::move(concatenated), etl::forward(ts)...); + return tuple_cat(private_tuple::tuple_cat_impl(etl::forward(t1), + etl::make_index_sequence>::value>{}, + etl::forward(t2), + etl::make_index_sequence>::value>{}), + etl::forward(ts)...); } #if ETL_USING_STL @@ -1317,7 +1317,7 @@ namespace std template struct tuple_element> { - using type = typename etl::nth_type_t; + using type = etl::nth_type_t; }; } diff --git a/test/test_tuple.cpp b/test/test_tuple.cpp index 918541e0..c7e7a667 100644 --- a/test/test_tuple.cpp +++ b/test/test_tuple.cpp @@ -35,6 +35,7 @@ SOFTWARE. #include #include +#include namespace { @@ -486,6 +487,56 @@ namespace CHECK_EQUAL(std::string("2"), d.value); } + //************************************************************************* + TEST(test_construct_from_rvalue_pair_implicit_conversion) + { + ETL_OR_STD::pair p(1, Data("2")); + + etl::tuple tp(etl::move(p)); + + int i = etl::get<0>(tp); + Data d = etl::get<1>(tp); + + CHECK_EQUAL(1, i); + CHECK_EQUAL(std::string("2"), d.value); + } + + //************************************************************************* + TEST(test_construct_from_rvalue_pair_explicit_conversion) + { + ETL_OR_STD::pair p(From(1), From(2)); + + etl::tuple tp(etl::move(p)); + + CHECK_EQUAL(1, etl::get<0>(tp).i); + CHECK_EQUAL(2, etl::get<1>(tp).i); + } + + //************************************************************************* + TEST(test_construct_from_const_rvalue_pair_implicit_conversion) + { + const ETL_OR_STD::pair p(1, Data("2")); + + etl::tuple tp(etl::move(p)); + + int i = etl::get<0>(tp); + Data d = etl::get<1>(tp); + + CHECK_EQUAL(1, i); + CHECK_EQUAL(std::string("2"), d.value); + } + + //************************************************************************* + TEST(test_construct_from_const_rvalue_pair_explicit_conversion) + { + const ETL_OR_STD::pair p(From(1), From(2)); + + etl::tuple tp(etl::move(p)); + + CHECK_EQUAL(1, etl::get<0>(tp).i); + CHECK_EQUAL(2, etl::get<1>(tp).i); + } + //************************************************************************* ETL_NODISCARD bool Get() { @@ -553,7 +604,7 @@ namespace } //************************************************************************* - TEST(test_tuple_cat) + TEST(test_tuple_cat_2) { etl::tuple tp1{1, 2.3}; etl::tuple tp2{4, Data("Data", 5)}; @@ -566,6 +617,46 @@ namespace CHECK_EQUAL(etl::get<3>(tp3), etl::get<1>(tp2)); } + //************************************************************************* + TEST(test_tuple_cat_3) + { + etl::tuple tp1{1, 2.3}; + etl::tuple tp2{4, Data("Data", 5)}; + etl::tuple tp3{true, 5}; + + auto tp4 = etl::tuple_cat(tp1, tp2, tp3); + static_assert(std::is_same>::value, "tp4 type mismatch"); + + CHECK_EQUAL(etl::get<0>(tp4), etl::get<0>(tp1)); + CHECK_EQUAL(etl::get<1>(tp4), etl::get<1>(tp1)); + CHECK_EQUAL(etl::get<2>(tp4), etl::get<0>(tp2)); + CHECK_EQUAL(etl::get<3>(tp4), etl::get<1>(tp2)); + CHECK_EQUAL(etl::get<4>(tp4), etl::get<0>(tp3)); + CHECK_EQUAL(etl::get<5>(tp4), etl::get<1>(tp3)); + } + + //************************************************************************* + TEST(test_tuple_cat_4) + { + etl::tuple tp1{1, 2.3}; + etl::tuple tp2{4, Data("Data", 5)}; + etl::tuple tp3{true, 5}; + etl::tuple tp4{1.01, 6, false}; + + auto tp5 = etl::tuple_cat(tp1, tp2, tp3, tp4); + static_assert(std::is_same>::value, "tp5 type mismatch"); + + CHECK_EQUAL(etl::get<0>(tp5), etl::get<0>(tp1)); + CHECK_EQUAL(etl::get<1>(tp5), etl::get<1>(tp1)); + CHECK_EQUAL(etl::get<2>(tp5), etl::get<0>(tp2)); + CHECK_EQUAL(etl::get<3>(tp5), etl::get<1>(tp2)); + CHECK_EQUAL(etl::get<4>(tp5), etl::get<0>(tp3)); + CHECK_EQUAL(etl::get<5>(tp5), etl::get<1>(tp3)); + CHECK_EQUAL(etl::get<6>(tp5), etl::get<0>(tp4)); + CHECK_EQUAL(etl::get<7>(tp5), etl::get<1>(tp4)); + CHECK_EQUAL(etl::get<8>(tp5), etl::get<2>(tp4)); + } + //************************************************************************* TEST(test_forward_as_tuple) { @@ -831,7 +922,7 @@ namespace etl::tuple tp(0, Data("")); - tp = static_cast&&>(p); + tp = etl::move(p); int i = etl::get<0>(tp); Data d = etl::get<1>(tp);