Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From the little research I did (and ignoring the c++0x comment from Johannes), my answer is that it depends what you want the <code>enable_if</code> for. If you want the conversion operation to <code>T</code> to exist or not from the type <code>T</code> then it seems that the answer is no, there is no way in C++03 (as Ugo said). But if you need the <code>enable_if</code> to change the <em>behavior</em> of the operator depending on the type of <code>T</code> then yes, there is a workaround which is to call an enabled helper function (called <code>to&lt;T&gt;</code> as Matthieu suggested).</p> <pre><code>#include&lt;iostream&gt; #include&lt;boost/utility/enable_if.hpp&gt; #include&lt;boost/type_traits/is_class.hpp&gt; struct B{ B(const B&amp; other){} B(){} }; struct A{ template&lt;class T&gt; T to(typename boost::enable_if_c&lt;not boost::is_class&lt;T&gt;::value, void*&gt;::type = 0){ std::clog &lt;&lt; "converted to non class" &lt;&lt; std::endl; return T(0); } template&lt;class T&gt; T to(typename boost::enable_if_c&lt;boost::is_class&lt;T&gt;::value, void*&gt;::type = 0){ std::clog &lt;&lt; "conveted to class" &lt;&lt; std::endl; return T(); } template&lt;class T&gt; operator T(){ return to&lt;T&gt;(); } }; int main(){ A a; double d = (double)a; // output: "converted to non class" B b = (B)(a); // output: "converted to class" return 0; } </code></pre> <p>For the record, I was frustrated with this for several days, until I realized that I wanted <code>enable_if</code> not for SFINAE but for compile-time behavior change. You may also find that this is the real reason for your need for <code>enable_if</code> also. Just a suggestion.</p> <p>(Please note that this is an answer for the C++98 era)</p>
 

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