CETL 2.1.0
 
Loading...
Searching...
No Matches
Example 10: Using CETL's unbounded_variant

Full example for cetl::unbounded_variant

#include <gmock/gmock.h>
TEST(example_10_unbounded_variant, basic_usage)
{
using ub_var = cetl::unbounded_variant<std::max(sizeof(int), sizeof(double))>;
ub_var a = 1;
EXPECT_THAT(cetl::get<int>(a), 1);
a = 3.14;
EXPECT_THAT(cetl::get<double>(a), 3.14);
a = true;
EXPECT_TRUE(cetl::get<bool>(a));
// bad any cast
//
a = 1;
#ifdefined(__cpp_exceptions)
// Workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
// Should be used in the tests where exceptions are expected (see `EXPECT_THROW`).
const auto sink = [](auto&&) {};
#else
EXPECT_THAT(cetl::get_if<float>(&a), testing::IsNull());
#endif
a = 2;
EXPECT_TRUE(a.has_value());
// reset
//
a.reset();
EXPECT_FALSE(a.has_value());
// pointer to contained data
//
a = 3;
EXPECT_THAT(*cetl::get_if<int>(&a), 3);
}
Defines a type of object to be thrown by the get on failure.
Definition unbounded_variant.hpp:31
The class unbounded_variant describes a type-safe container for single values of unbounded_variant co...
Definition unbounded_variant.hpp:1029
T max(T... args)
CETL_NODISCARD std::add_pointer_t< std::add_const_t< ValueType > > get_if(const UbVar< Footprint, Copyable, Movable, Alignment, Pmr > *const operand) noexcept
Performs type-safe access to the const contained object.
Definition unbounded_variant.hpp:1652
CETL_NODISCARD const ValueType & get(const UbVar< Footprint, Copyable, Movable, Alignment, Pmr > &operand)
Performs type-safe access to the contained object.
Definition unbounded_variant.hpp:1721
Includes cetl::unbounded_variant type and non-member functions.