title: Clang vs GCC vs MSVC template conversion operator - which compiler is right?
body: <p>I have simple code with conversion operator and it seems like all compilers are giving different results, was curious which compiler, if any, is correct?
  I tried different combinations as well, but below ones are the most interesting. Code was compiled using C++11 flag, but the same behavior might be observed in C++03 as well.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  struct call_operator {
      template&lt;typename T&gt;
      operator T() {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
          return {};
      }
  
      template&lt;typename T&gt;
      operator const T&amp;() const {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
          static T t;
          return t;
      }
  
      template&lt;typename T&gt;
      operator T&amp;() const {
          std::cout &lt;&lt; __FUNCTION__ &lt;&lt; std::endl;
          static T t;
          return t;
      }
  };
  
  int main() {
      (void)static_cast&lt;int&gt;(call_operator());
      (void)static_cast&lt;const int&amp;&gt;(call_operator());
      (void)static_cast&lt;int&amp;&gt;(call_operator());
  }
  </code></pre>
  
  <p>clang-3.6:</p>
  
  <pre><code>operator int
  operator const int &amp;
  operator int &amp;
  </code></pre>
  
  <p>g++-4.9:</p>
  
  <pre><code>operator T
  operator const T&amp;
  operator T&amp;
  </code></pre>
  
  <p>msvc 2014 CTP:</p>
  
  <pre><code>call_operator.cpp(17): error C2440: 'static_cast': cannot convert from 'call_operator' to ' const int &amp;'
  </code></pre>
  
  <p>after removal of:</p>
  
  <pre><code>template&lt;typename T&gt;
  operator T();
  </code></pre>
  
  <p>msvc compiles:</p>
  
  <pre><code>call_operator::operator const int &amp;
  call_operator::operator const int &amp;
  call_operator::operator int &amp;
  </code></pre>
  
  <p>furthermore, after removal of const in </p>
  
  <pre><code>template&lt;typename T&gt;
  operator const T&amp;();
  </code></pre>
  
  <p>clang-3.6:</p>
  
  <pre><code>call_operator.cpp:26:9: error: ambiguous conversion for static_cast from 'call_operator' to 'int' (void)static_cast&lt;int&gt;(call_operator());
  </code></pre>
  
  <p>g++-4.9:</p>
  
  <pre><code>operator T
  operator const T&amp;
  operator T&amp;
  </code></pre>
  
  <p>msvc 2014 CTP:</p>
  
  <pre><code>call_operator.cpp(16): error C2440: 'static_cast': cannot convert from 'call_operator' to 'int'
  </code></pre>