#1009 etl::expected doesn't compile with legacy_variant

Added in_place constructor
This commit is contained in:
John Wellbelove 2025-02-01 08:59:20 +00:00
parent bbaa60b382
commit fcbbbeaf44
2 changed files with 42 additions and 1 deletions

View File

@ -480,6 +480,19 @@ namespace etl
type_id = Type_Id_Lookup<T>::type_id;
}
//***************************************************************************
/// Constructor that catches any types that are not supported.
/// Forces a ETL_STATIC_ASSERT.
//***************************************************************************
template <size_t Index, typename T>
explicit variant(etl::in_place_index_t<Index>, T const& value)
: type_id(Index)
{
ETL_STATIC_ASSERT(Type_Id_Lookup<T>::type_id == Index, "Missmatched type");
::new (static_cast<T*>(data)) T(value);
type_id = Index;
}
//***************************************************************************
/// Copy constructor.
///\param other The other variant object to copy.

View File

@ -204,7 +204,7 @@ namespace
typedef etl::legacy::variant<D1, D2, D3, D4> test_variant_emplace;
SUITE(test_variant)
SUITE(test_variant_legacy)
{
TEST(test_alignment)
{
@ -258,6 +258,34 @@ namespace
CHECK_EQUAL(text, variant_text.get<std::string>());
}
//*************************************************************************
TEST(test_constructor_in_place_value)
{
// Char.
char c = 'a';
test_variant_3a variant_char(etl::in_place_index_t<0>(), c);
CHECK(variant_char.is_type<char>());
CHECK(variant_char.is_valid());
CHECK_EQUAL(c, variant_char.get<char>());
// Int.
int i = 1;
test_variant_3a variant_int(etl::in_place_index_t<1>(), i);
CHECK(variant_int.is_type<int>());
CHECK(variant_int.is_valid());
CHECK_EQUAL(i, variant_int.get<int>());
// String.
std::string text("Some Text");
test_variant_3a variant_text(etl::in_place_index_t<2>(), text);
CHECK(variant_text.is_type<std::string>());
CHECK(variant_text.is_valid());
CHECK_EQUAL(text, variant_text.get<std::string>());
}
//*************************************************************************
TEST(test_copy_constructor)
{