Allow easier construction of const_(multi_)map (#1561)

When forcing to feed into value_types it has to be an
ETL_OR_STD::pair<const K, V>, but when you want to use CTAD on the pairs the
const is missing and the assert triggers.
This commit is contained in:
Björn Schäpers 2026-08-22 09:02:39 +02:00 committed by GitHub
parent 266b0f99c6
commit ed6c46bf7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 4 deletions

View File

@ -508,7 +508,8 @@ namespace etl
//*************************************************************************
///\brief Construct a const_map from a variadic list of elements.
/// Static asserts if the elements are not of type <code>value_type</code>.
/// Static asserts if the elements are not constructible into
/// <code>value_type</code>.
/// Static asserts if the number of elements is greater than the capacity of
/// the const_map.
//*************************************************************************
@ -517,7 +518,8 @@ namespace etl
: iconst_map<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
, element_list{etl::forward<TElements>(elements)...}
{
static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
static_assert((etl::conjunction<etl::is_constructible<value_type, etl::decay_t<TElements>>...>::value),
"All elements must be constructible into value_type");
static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
}

View File

@ -458,7 +458,8 @@ namespace etl
//*************************************************************************
///\brief Construct a const_map from a variadic list of elements.
/// Static asserts if the element type is not constructible.
/// Static asserts if the elements are not of type <code>value_type</code>.
/// Static asserts if the elements are not constructible into
/// <code>value_type</code>.
/// Static asserts if the number of elements is greater than the capacity of
/// the const_map.
//*************************************************************************
@ -467,7 +468,8 @@ namespace etl
: iconst_multimap<TKey, TMapped, TKeyCompare>(element_list, sizeof...(elements), Size)
, element_list{etl::forward<TElements>(elements)...}
{
static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be value_type");
static_assert((etl::conjunction<etl::is_constructible<value_type, etl::decay_t<TElements>>...>::value),
"All elements must be constructible into value_type");
static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
}

View File

@ -257,6 +257,27 @@ namespace
CHECK_TRUE(data.max_size() == Max_Size);
CHECK_FALSE(data.begin() == data.end());
}
//*************************************************************************
TEST(test_cpp17_deduced_constructor_of_plain_pair)
{
static const etl::const_map data{ETL_OR_STD::pair{Key('A'), 0}, ETL_OR_STD::pair{Key('B'), 1}, ETL_OR_STD::pair{Key('C'), 2},
ETL_OR_STD::pair{Key('D'), 3}, ETL_OR_STD::pair{Key('E'), 4}, ETL_OR_STD::pair{Key('F'), 5},
ETL_OR_STD::pair{Key('G'), 6}, ETL_OR_STD::pair{Key('H'), 7}, ETL_OR_STD::pair{Key('I'), 8},
ETL_OR_STD::pair{Key('J'), 9}};
etl::const_map<Key, int, 10U> check{ETL_OR_STD::pair{Key('A'), 0}, ETL_OR_STD::pair{Key('B'), 1}, ETL_OR_STD::pair{Key('C'), 2},
ETL_OR_STD::pair{Key('D'), 3}, ETL_OR_STD::pair{Key('E'), 4}, ETL_OR_STD::pair{Key('F'), 5},
ETL_OR_STD::pair{Key('G'), 6}, ETL_OR_STD::pair{Key('H'), 7}, ETL_OR_STD::pair{Key('I'), 8},
ETL_OR_STD::pair{Key('J'), 9}};
CHECK_TRUE(data.is_valid());
CHECK_TRUE(data.size() == Max_Size);
CHECK_FALSE(data.empty());
CHECK_TRUE(data.full());
CHECK_TRUE(data.capacity() == Max_Size);
CHECK_TRUE(data.max_size() == Max_Size);
CHECK_FALSE(data.begin() == data.end());
}
#endif
//*************************************************************************

View File

@ -226,6 +226,28 @@ namespace
CHECK_TRUE(data.max_size() == Max_Size);
CHECK_FALSE(data.begin() == data.end());
}
//*************************************************************************
TEST(test_cpp17_deduced_constructor_of_plain_pair)
{
static const etl::const_multimap data{ETL_OR_STD::pair{Key('A'), 0}, ETL_OR_STD::pair{Key('B'), 1}, ETL_OR_STD::pair{Key('C'), 2},
ETL_OR_STD::pair{Key('D'), 3}, ETL_OR_STD::pair{Key('E'), 4}, ETL_OR_STD::pair{Key('F'), 5},
ETL_OR_STD::pair{Key('G'), 6}, ETL_OR_STD::pair{Key('G'), 7}, ETL_OR_STD::pair{Key('G'), 8},
ETL_OR_STD::pair{Key('J'), 9}};
etl::const_multimap<Key, int, 10U> check{ETL_OR_STD::pair{Key('A'), 0}, ETL_OR_STD::pair{Key('B'), 1}, ETL_OR_STD::pair{Key('C'), 2},
ETL_OR_STD::pair{Key('D'), 3}, ETL_OR_STD::pair{Key('E'), 4}, ETL_OR_STD::pair{Key('F'), 5},
ETL_OR_STD::pair{Key('G'), 6}, ETL_OR_STD::pair{Key('H'), 7}, ETL_OR_STD::pair{Key('I'), 8},
ETL_OR_STD::pair{Key('J'), 9}};
CHECK_TRUE(data.is_valid());
CHECK_TRUE(data.size() == Max_Size);
CHECK_FALSE(data.empty());
CHECK_TRUE(data.full());
CHECK_TRUE(data.capacity() == Max_Size);
CHECK_TRUE(data.max_size() == Max_Size);
CHECK_FALSE(data.begin() == data.end());
}
#endif
//*************************************************************************