title: How to implement generic max function?
body: <p>I know this is because the return type of the template function is same as that of the first argument (T).<br>
  How can I modify this template so that, it behaves correctly in all cases?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  template &lt;typename T, typename U&gt;
  T max(T x, U y)
  {
      return x&gt;y ? x : y;
  }
  
  int main()
  {
      cout&lt;&lt;max(17.9,17)&lt;&lt;"\n";
      cout&lt;&lt;max(17,17.9)&lt;&lt;"\n";
  }
  </code></pre>
  
  <p><strong>Output:</strong>  </p>
  
  <pre><code>17.9  
  17
  </code></pre>