#Question about Enum Classes

9 messages · Page 1 of 1 (latest)

tranquil stone
#
enum class NewType : int
{ APPLE, BANANA, ORANGE, MANGO, STRAWBERRY, CHERRY, WATERMELON }

void Foo::serialize_this(Serialize& serialize) 
{
serialize.get_int(static_cast<std::underlying_type_t<NewType>>(NewType::APPLE));

serialize.get_int(static_cast<std::underlying_type_t<NewType>>(NewType::BANANA));

serialize.get_int(static_cast<std::underlying_type_t<NewType>>(NewType::MANGO));
}

Hi can you please hint me how to remove the code repetition here and
static_cast<std::underlying_type_t<NewType>>(NewType::APPLE) Shorten this expression ?

unique briarBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

zealous hemlock
#

Write a function/template and can that instead
C++23 will introduce a std::to_underlying to the standard library, but you can roll your own version

#

Alternatively since you kinda need to know that the underlying type is int to call the right function on your serializer, just cast to int directly

#

Alternatively, write a "serialize enum" function for your serializer

tranquil stone
#

@zealous hemlock thank you

#

!solved

unique briarBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] Question about Enum Classes