Channels ▼

Loops, Metaloops, & C++

August, 2004: Loops, Metaloops, & C++

Listing 10

// Using For<> to calculate the square root

template<int number, int low_ = 0 , int high_ = number>
struct sqrt_state
     enum { num = number }; // doesn't change
     enum { low = low_ };
     enum { high = high_ };
template<typename state>
struct sqrt_cond
      enum { result =  state::low < state::high };
template <typename state>
struct sqrt_incr
       typedef state result ;      // state is left unchanged
// slice the range into two halves and decide in which half 
// to continue search
template<typename state>
struct sqrt_body
         enum { mid = state::low + (state::high - state::low) /2 };
         enum { in_first_half =  mid * mid >= state::num } ;
         typedef sqrt_state<state::num, state::low, mid>  first_half;
         typedef sqrt_state<state::num, mid+1, state::high>  sec_half;
         typedef typename If_else< in_first_half, first_half, 
                                            sec_half >::result result;
// compute square root of N
template <int N>
struct sqroot
   enum { result =  For<sqrt_cond, sqrt_incr, 
                             sqrt_body, sqrt_state<N> >::result::low } ;
cout << sqroot<16>::result ;

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.