dimanche 22 novembre 2015

Calling an operator over a series of members variables

I am looking for a method to call an operator over a series of member variables. My goal is to reduce the number of references to each variant to exactly 1. The following MWE illustrates what I have, and what I am trying to achieve.

Valid c++1z:

template<typename... Args> bool all(Args... args) { return (args && ...); }

struct foo {
  std::string m1;
  std::string m2;
  int m3;
  double m4;

  bool compare(const foo& rhs) const {
    return all(m1 == rhs.m1, m2 == rhs.m2, m3 == rhs.m3, m4 == rhs.m4);
  }
}

What I would like to achieve is something more of the form:

...
return all<*this, rhs>(m1, m2, m3, m4);

The exact syntax of the second form does not matter. However I am not looking for macro or "just write {return *this == rhs;} answers. There is no need to use modern features if they can be avoided.





Aucun commentaire:

Enregistrer un commentaire