Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For your request to do </p> <pre><code>if (InSet(value)(GetValue1(), GetValue2(), GetValue3())) { // Do something here... } </code></pre> <p>Try this:</p> <pre><code>template &lt;typename T&gt; class InSetHelper { const T &amp;Value; void operator=(const InSetHelper &amp;); public: InSetHelper(const T &amp;value) : Value(value) {} template&lt;class Other, class Another&gt; bool operator()(const Other &amp;value1, const Another &amp;value2) const { return Value == value1 || Value == value2; } template&lt;class Other, class Another, class AThird&gt; bool operator()(const Other &amp;value1, const Another &amp;value2, const AThird &amp;value3) const { return Value == value1 || Value == value2 || Value == value3; } }; template &lt;typename T&gt; InSetHelper&lt;T&gt; InSet(const T &amp;value) { return InSetHelper&lt;T&gt;(value); } </code></pre> <p>This syntax might be more clear though:</p> <pre><code>if (MakeSet(GetValue1(), GetValue2(), GetValue3()).Contains(value)) { // Do something here... } template &lt;typename T, typename U, typename V&gt; class Set3 { const T&amp; V1; const U&amp; V2; const V&amp; V3; void operator=(const Set3 &amp;); public: Set3(const T &amp;v1, const U &amp;v2, const V &amp;v3) : V1(v1), V2(v2), V3(v3) {} template &lt;typename W&gt; bool Contains(const W &amp;v) const { return V1 == v || V2 == v || V3 == v; } }; template &lt;typename T, typename U&gt; class Set2 { // as above }; template &lt;typename T, typename U, typename V&gt; Set3&lt;T, U, V&gt; MakeSet(const T &amp;v1, const U &amp;v2, const V &amp;v3) { return Set3&lt;T, U, V&gt;(v1, v2, v3); } template &lt;typename T, typename U&gt; Set3&lt;T, U&gt; MakeSet(const T &amp;v1, const U &amp;v23) { return Set3&lt;T, U, V&gt;(v1, v2); } </code></pre> <p>If those values are really part of a tree or a linked list, then you have your set/container already, and your best bet is to just use some recursion:</p> <pre><code>parent.ThisOrDescendantHasValue(value); </code></pre> <p>You'd just add this to whatever class parent and child belong to:</p> <pre><code>class Node { public: Value GetValue(); Node *GetChild(); bool ThisOrDescendantHasValue(const Value &amp;value) { return GetValue() == value || (GetChild() &amp;&amp; GetChild-&gt;ThisOrDescendantHasValue(value)); } }; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload