C++17 Utility classes for comparing multiple values in one simple expression
Switch branches/tags
Nothing to show
Clone or download

README.md

dry-comparisons

This is the source code for the any_of, all_of and none_of C++17 utility mentioned in the blog post "DRY multicomparisons"

Public domain.

Usage

using rollbear::any_of;

class Machine
{
public:
  ...
  void func() {
    assert(state == any_of(S1, S3)); // assert(state == S1 || state == S3);
    ...
  }
private:
  enum { S0, S1, S2, S3 } state;
};

The types available are any_of, all_of and none_of.

iff all member types can be inserted into an ostream, an instance of any_of, all_of or none_of may be printed, e.g. std::cout << rollbear::any_of{1,3,5}, which will produce the output any_of{1,3,5}.

iff it is possible to do logical and/or operations on all members, instances of any_of, all_of and none_of may be used directly as boolean expressions.

  • bool(any_of{a,b,c}) == bool(a || b || c)
  • bool(all_of{a,b,c}) == bool(a && b && c)
  • bool(none_of{a,b,c}) == !bool(a || b || c)

It is also possible to do relational comparisons. For example:

assert(all_of(s1, s2) > "."), providing the expressions s1 and s2 are greater-than comparable with a string literal.