Proper usage of Comma fold in parameter pack C++

42 views Asked by At

I have a class with a variadic argument templated constructor and I am trying to separate out the parameter pack one by one and pass it to member set function. I tried std::move and std::forward as well as shown in commented lines.

class Builder {
public:
  template <class... Args> Builder(Args... args) {
    // (set(std::forward<Args>(args)), ...);
    // (set(std::move(args)), ...);
    (set(args), ...);
  }

  void set() {}

  void set(int x) { X = x; }

  void set(float y) { Y = y; }

  void set(std::string str) { name = str; }

private:
  int X;
  float Y;
  std::string name;
};

int main() {
  Builder bld1{1, 2.5, "Hello"};
  return 0;
}

I get an error saying -

call to member function 'set' is ambiguous
    (set(args), ...);

Trying to follow the fold expressions documentation and other online posts, but could not get this resolved.

0

There are 0 answers