Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>template &lt;int number1&gt; typename boost::enable_if_c&lt; (number1 &gt;= 10) &gt;::type reportErrorIfLessThan10() { // ... } </code></pre> <p>The above <code>enable_if</code>, without the _c because we have a plain bool, looks like this:</p> <pre><code>template&lt;bool C, typename T = void&gt; struct enable_if { typedef T type; }; template&lt;typename T&gt; struct enable_if&lt;false, T&gt; { }; </code></pre> <p><a href="http://en.wikipedia.org/wiki/Boost_C%2B%2B_Libraries" rel="nofollow noreferrer">Boost</a>'s <code>enable_if</code> takes not a plain bool, so they have another version which has a _c appended, that takes plain bools. You won't be able to call it for <code>number1</code> &lt; 10. <em>SFINAE</em> will exclude that template as possible candidates, because <code>enable_if</code> will not expose a type <code>::type</code> if the condition evaluates to <code>false</code>. If you want, for some reason, test it in the function, then if you have the <a href="http://en.wikipedia.org/wiki/C%2B%2B0x" rel="nofollow noreferrer">C++1x</a> feature available, you can use <code>static_assert</code>:</p> <pre><code>template &lt;int number1&gt; void reportErrorIfLessThan10() { static_assert(number &gt;= 10, "number must be &gt;= 10"); } </code></pre> <p>If not, you can use BOOST_STATIC_ASSERT:</p> <pre><code>template &lt;int number1&gt; void reportErrorIfLessThan10() { BOOST_STATIC_ASSERT(number &gt;= 10); } </code></pre> <p>The only way to display a descriptive message is using static_assert, though. You can more or less simulate that, using types having names that describe the error condition:</p> <pre><code>namespace detail { /* chooses type A if cond == true, chooses type B if cond == false */ template &lt;bool cond, typename A, typename B&gt; struct Condition { typedef A type; }; template &lt;typename A, typename B&gt; struct Condition&lt;false, A, B&gt; { typedef B type; }; struct number1_greater_than_10; } template &lt;int number1&gt; void reportErrorIfLessThan10() { // number1 must be greater than 10 sizeof( typename detail::Condition&lt; (number1 &gt;= 10), char, detail::number1_greater_than_10 &gt;::type ); } </code></pre> <p>It prints this here:</p> <blockquote> <p>error: invalid application of 'sizeof' to incomplete type 'detail::number1_greater_than_10' </p> </blockquote> <p>But I think the very first approach, using <code>enable_if</code> will do it. You will get an error message about an undeclared <code>reportErrorIfLessThan10</code>.</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.
    2. 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