mercredi 5 février 2020

helper function to check a value is any of its parameters

I need to check if a value of type T is any of its parameters using a helper function.

For example, using something like the following code

enum class my_enum{k1, k2, k3, k4, k5};
auto v{my_enum::k1};
if (is_any_of(v, my_enum::k1, my_enum::k4, my_enum::k5)) {
}

rather than using if (v == my_enum::k1 || v == my_enum::k4 || v== my_enum::k5) {} or using switch-case.

How do I implement the variadic function bool is_any_of() in C++11?

How the implementation would become simpler with C++17 fold expressions?

Aucun commentaire:

Enregistrer un commentaire