For your request
if (InSet(value)(GetValue1(), GetValue2(), GetValue3())) {
Try the following:
template <typename T> class InSetHelper { const T &Value; void operator=(const InSetHelper &); public: InSetHelper(const T &value) : Value(value) {} template<class Other, class Another> bool operator()(const Other &value1, const Another &value2) const { return Value == value1 || Value == value2; } template<class Other, class Another, class AThird> bool operator()(const Other &value1, const Another &value2, const AThird &value3) const { return Value == value1 || Value == value2 || Value == value3; } }; template <typename T> InSetHelper<T> InSet(const T &value) { return InSetHelper<T>(value); }
This syntax may be more clear:
if (MakeSet(GetValue1(), GetValue2(), GetValue3()).Contains(value)) { // Do something here... } template <typename T, typename U, typename V> class Set3 { const T& V1; const U& V2; const V& V3; void operator=(const Set3 &); public: Set3(const T &v1, const U &v2, const V &v3) : V1(v1), V2(v2), V3(v3) {} template <typename W> bool Contains(const W &v) const { return V1 == v || V2 == v || V3 == v; } }; template <typename T, typename U> class Set2 { // as above }; template <typename T, typename U, typename V> Set3<T, U, V> MakeSet(const T &v1, const U &v2, const V &v3) { return Set3<T, U, V>(v1, v2, v3); } template <typename T, typename U> Set3<T, U> MakeSet(const T &v1, const U &v23) { return Set3<T, U, V>(v1, v2); }
If these values are indeed part of a tree or linked list, you already have your own set / container, and it is best to use some recursion:
parent.ThisOrDescendantHasValue(value);
You would just add this to what class the parent and child belong to:
class Node { public: Value GetValue(); Node *GetChild(); bool ThisOrDescendantHasValue(const Value &value) { return GetValue() == value || (GetChild() && GetChild->ThisOrDescendantHasValue(value)); } };