Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking that a template type T is part of a variadic parameter pack in C++17

I want to check that a type T is also part of a parameter pack Ts. There are solutions that do that in C++14, but I'm wandering if this can be simplified in C++17. If T is not found in Tsthe compiler should stop (static_assertion should fail).

template<typename... Ts>
class A
{
  template<typename T>
  void action() {
    // check that T is also in Ts (static_assertion)
  }
}
like image 522
dani Avatar asked Dec 19 '22 05:12

dani


1 Answers

I hear fold-expressions are the new hotness:

static_assert((std::is_same_v<T, Ts> || ...));
like image 58
Barry Avatar answered Dec 24 '22 02:12

Barry