Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional enable an alternative assignment operator
    text
    copied!<p>I'm trying to conditionally instantiate an extra assignment operator. The code below works fine in clang, but not in gcc 4.7.</p> <p>The problem I'm having seems very similar the the question asked here: <a href="https://stackoverflow.com/questions/6972368/stdenable-if-to-conditionally-compile-a-member-function">std::enable_if to conditionally compile a member function</a></p> <p>The following illustrates the problem I'm having:</p> <pre><code>#include &lt;type_traits&gt; template&lt;typename T&gt; struct StrangerTypeRules; template&lt;typename T&gt; struct X; template&lt; &gt; struct StrangerTypeRules &lt; unsigned &gt; { typedef unsigned type; }; template&lt; &gt; struct StrangerTypeRules &lt; bool &gt; { typedef X&lt;bool&gt; type; }; template&lt;typename T&gt; struct X { // In the non-trivial version of my code, I can not use the // default assignment operator, therefor I need to define this one X&amp; operator=( const X&lt;T&gt;&amp; rhs ) { return *this; } // Alternative assignment oprtator, must only exists if it is // different from the assignment operator above template&lt;typename = typename std::enable_if&lt; ( !std::is_same&lt; X&lt;T&gt;, typename StrangerTypeRules&lt;T&gt;::type &gt;::value ), X&lt;T&gt; &gt;::type &gt; X&lt;T&gt; &amp; operator=( const typename StrangerTypeRules &lt;T&gt;::type&amp; rhs ) { return *this; } }; int main(int argc, const char *argv[]) { X&lt;unsigned&gt; x1, x2; x1 = 4; x2 = x1; X&lt;bool&gt; x3, x4; // compile error in gcc 4.7 with -std=c++11 //x3 = x4; return 0; } </code></pre> <p>Can this be done in a way which satisfies both clang and gcc 4.7? If so, how?</p> <p>Compilations error when using gcc:</p> <pre><code>test.cxx: In instantiation of ‘struct X&lt;bool&gt;’: test.cxx:52:13: required from here test.cxx:38:12: error: no type named ‘type’ in ‘struct std::enable_if&lt;false, X&lt;bool&gt; &gt;’ </code></pre>
 

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