Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange error with a templated operator overload
    primarykey
    data
    text
    <p>When I compile the following snippet, I get a compiler error with clang, but not with g++/MSVC:</p> <pre><code>#include &lt;string&gt; template&lt;typename T&gt; struct Const { explicit Const(T val) : value(val) {} T value; }; template&lt;typename T&gt; struct Var { explicit Var(const std::string &amp;n) : name(n) {} std::string name; }; template&lt;typename L, typename R&gt; struct Greater { Greater(L lhs, R rhs) : left(lhs), right(rhs) {} L left; R right; }; template&lt;typename L&gt; Greater&lt;L, Const&lt;int&gt; &gt; operator &gt; (L lhs, int rhs) { return Greater&lt;L, Const&lt;int&gt; &gt;(lhs, Const&lt;int&gt;(rhs)); } template&lt;typename R&gt; Greater&lt;Const&lt;int&gt;, R&gt; operator &gt; (int lhs, R rhs) { return Greater&lt;Const&lt;int&gt;, R&gt;(Const&lt;int&gt;(lhs), rhs); } Var&lt;double&gt; d("d"); int main() { d &gt; 10; return 0; } </code></pre> <p>The error reported is the following:</p> <pre><code>error: overloaded 'operator&gt;' must have at least one parameter of class or enumeration type Greater&lt;Const&lt;int&gt;, R&gt; operator &gt; (int lhs, R rhs) { ^ ./val.h:31:24: note: in instantiation of function template specialization 'operator&gt;&lt;int&gt;' requested here Greater&lt;Const&lt;int&gt;, R&gt; operator &gt; (int lhs, R rhs) { ^ 1 error generated. </code></pre> <p>which is about the operator function that is not used. If, instead, I write 10 > d instead of d > 10, then I get the same error about the other operator > function. The above compiles fine under gcc 4.4.6 and VS2012. What is my mistake ? </p> <p>Thank you. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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