Skip to content

Instantly share code, notes, and snippets.

@rioki
Created April 26, 2022 07:39
Show Gist options
  • Save rioki/72fcd7b9b661e85967cb3dd039b5287f to your computer and use it in GitHub Desktop.
Save rioki/72fcd7b9b661e85967cb3dd039b5287f to your computer and use it in GitHub Desktop.
one_of: a small piece of code that removes switch on enum statements.
template <typename EnumT> constexpr
bool one_of(EnumT value, const std::initializer_list<EnumT>& checkValues)
{
for (const auto& cv : checkValues)
{
if (value == cv)
{
return true;
}
}
return false;
}
enum class TestEnum
{
A,
B,
C
};
TEST(one_of, one_of)
{
EXPECT_TRUE(one_of(TestEnum::A, {TestEnum::A, TestEnum::B}));
EXPECT_TRUE(one_of(TestEnum::B, {TestEnum::A, TestEnum::B}));
EXPECT_FALSE(one_of(TestEnum::C, {TestEnum::A, TestEnum::B}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment