Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should try and follow <a href="https://stackoverflow.com/questions/10283610/how-to-change-a-template-method-based-on-whether-the-type-is-an-integral-or-floa/10284176#comment13229539_10283610">@DavidRodriguez's advice</a> and separate the common functionality of your class into a base class; then provide specializations of the whole derived class where functionality differs. That would be the better approach.</p> <p>If you do want to keep your current implementation, you can SFINAE the undesired version of the operator out of overload resolution as follows:</p> <pre><code>#include &lt;iostream&gt; #include &lt;type_traits&gt; template&lt;class T&gt; struct Matrix { // ... }; template&lt;class T&gt; typename std::enable_if&lt; std::is_integral&lt;T&gt;::value, bool &gt;::type operator==( const Matrix&lt;T&gt;&amp;, const Matrix&lt;T&gt;&amp; ) { std::cout &lt;&lt; "Integer version" &lt;&lt; std::endl; return true; } template&lt;class T&gt; typename std::enable_if&lt; !std::is_integral&lt;T&gt;::value, bool &gt;::type operator==( const Matrix&lt;T&gt;&amp;, const Matrix&lt;T&gt;&amp; ) { std::cout &lt;&lt; "Floating point version" &lt;&lt; std::endl; return true; } int main() { Matrix&lt;int&gt; m1, m2; Matrix&lt;double&gt; m3, m4; if( m1 == m2 ) {} if( m3 == m4 ) {} } </code></pre> <p>If you want the operators as member functions the only way I know of to make it work is to add a dummy template parameter to the operators and add <code>&amp;&amp; std::is_same&lt;T,U&gt;</code> to the <code>std::enable_if</code> test condition.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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