title: What makes an overloaded function prefered to another?
body: <p>I have implemented a simple <code>BooleanVariable</code> class, where I get the boolean value from template parameter <code>V</code>, and it's comparison function <code>operator==</code>:</p>
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;bool V&gt;
  struct BooleanVariable {
      BooleanVariable() = default;
  
      constexpr bool operator()() const {
          return V;
      }
  };
  
  template&lt;bool V&gt;
  bool operator==(const BooleanVariable&lt;V&gt; &amp;, const BooleanVariable&lt;V&gt; &amp;) {
      return true;
  }
  
  template&lt;bool V1, bool V2&gt;
  bool operator==(const BooleanVariable&lt;V1&gt; &amp;, const BooleanVariable&lt;V2&gt; &amp;) {
      return false;
  }
  
  int main() {
      std::cout&lt;&lt;(BooleanVariable&lt;true&gt;() == BooleanVariable&lt;false&gt;())&lt;&lt;&quot;\n&quot;;
      std::cout&lt;&lt;(BooleanVariable&lt;false&gt;() == BooleanVariable&lt;true&gt;())&lt;&lt;&quot;\n&quot;;
      std::cout&lt;&lt;(BooleanVariable&lt;true&gt;() == BooleanVariable&lt;true&gt;())&lt;&lt;&quot;\n&quot;;
      std::cout&lt;&lt;(BooleanVariable&lt;false&gt;() == BooleanVariable&lt;false&gt;())&lt;&lt;&quot;\n&quot;;
      
      return 0;
  }
  </code></pre>
  <p><code>operator==</code> works perfectly, that I wonder why these 2 comparison functions don't have ambiguity problem for the case when <code>V1 == V2</code>, as both of fit to the template. Why is</p>
  <pre><code>template&lt;bool V1, bool V2&gt;
  bool operator==(const BooleanVariable&lt;V1&gt; &amp;, const BooleanVariable&lt;V2&gt; &amp;) {
      return false;
  }
  </code></pre>
  <p>not triggered when <code>V1 == V2</code>? Can someone explains to me?</p>