Do not enforce default constructability for const_(map|set) values

There is an actual use case to use types, which are not default constructible.
Change the assert in a way, that only iff not all elements in the internal
array aren't initialized there is a default constructor.

Fixes #1585.
This commit is contained in:
Björn Schäpers 2026-09-22 12:00:46 +02:00
parent 22bdef2ee4
commit 7ea723c077
2 changed files with 20 additions and 4 deletions

View File

@ -401,8 +401,6 @@ namespace etl
/// Defines the parameter types
using const_key_reference = const key_type&;
static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
#include "private/diagnostic_uninitialized_push.h"
//*************************************************************************
///\brief Construct a const_set from a variadic list of elements.
@ -420,6 +418,9 @@ namespace etl
{
static_assert((etl::are_all_same<value_type, etl::decay_t<TElements>...>::value), "All elements must be key_type");
static_assert(sizeof...(elements) <= Size, "Number of elements exceeds capacity");
static_assert(sizeof...(elements) == Size || etl::is_default_constructible<key_type>::value,
"key_type must be default constructible, if providing less than Size elements");
}
#include "private/diagnostic_pop.h"
@ -454,8 +455,6 @@ namespace etl
using const_iterator = typename base_t::const_iterator;
using size_type = typename base_t::size_type;
static_assert((etl::is_default_constructible<key_type>::value), "key_type must be default constructible");
//*************************************************************************
///\brief Default construct a const_set.
//*************************************************************************

View File

@ -1381,5 +1381,22 @@ namespace
CHECK_TRUE(greater_than_equal11);
#endif
}
//*************************************************************************
TEST(test_construct_from_not_default_constuctible)
{
struct F : Key
{
constexpr explicit F(char c)
: Key(c)
{
}
};
using Set = etl::const_set<F, 3>;
static const Set set{F('1'), F('2'), F('3')};
CHECK_TRUE(set.is_valid());
}
}
} // namespace