0

So, I already have a workable solution, but i want to know if there is any other way to handle this, in case i'm missing something obvious and simple.

What i want to express

if((a==c)||...)

where c is a parameter pack and a is a variable. Basically I want it expanded to

if( (a == c1) || (a == c2) ... etc)

As a MRE

  template <typename A, typename... C>
  void foo(A a, C... c) const
  {
      if((a == c)||...)
        return;
  }

The solution i ended up going with was

if (
    [](auto a, auto c1, auto c2)
      {
        return a.value == c1 || a.value == c2;
      }(a, c...))
Taekahn
  • 1,592
  • 1
  • 13
  • 16
  • Should there be any type constraints? Should the `C`s be the same type, or do you expect different types for each of them? Should they be implicitly converted to `A` when doing equality check, or should each of them have an `operator==(A, C)` defined? – Ranoiaetep Apr 21 '22 at 02:58
  • They are all of the same type, the same type as `A`. Simple intrinsic types – Taekahn Apr 21 '22 at 02:59
  • Also, the last snippet you showed wouldn't work if you have more than 2 `c`s. – Ranoiaetep Apr 21 '22 at 03:01
  • @Ranoiaetep yeah :( – Taekahn Apr 21 '22 at 03:05

1 Answers1

3

What i want to express

if((a==c)||...)

where c is a parameter pack and a is a variable. Basically I want it expanded to

if( (a == c1) || (a == c2) ... etc)

C++17 fold expression should be enough, which will expand what you expect

template <typename A, typename... C>
  void foo(A a, C... c) 
  {
    if(((a == c)|| ...))
      return;
  }

Demo

康桓瑋
  • 33,481
  • 5
  • 40
  • 90
  • Does that apply to the conditions of `constexpr`? Here in the following post: [What's the difference between `constexpr` and `const`?](https://stackoverflow.com/questions/14116003/whats-the-difference-between-constexpr-and-const) it says -> ***"Apart from typedefs and static asserts, only a single return statement is allowed."*** – Const Apr 21 '22 at 08:23
  • @Const That was a restriction in c++11 and was relaxed/removed since c++14. – VainMan Apr 21 '22 at 11:44