check for equality against multiple values in c

Yes, you can indeed write a variadic template function, combined with a fold-expression, like this:

namespace my 
{
  template<typename T, typename ... Vals>
  bool any_of(T t, Vals ...vals)
  {
     return (... || (t == vals));
  }
}  

and then use it like this:

if (my::any_of(some_function(), 2, 3, 5, 7, 11))
{
    do_something();
}

Note that I’ve put any_of in a namespace so as to avoid any confusion with std::any_of which is a completely different function.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top