Standard deviation in one pass in C++ (See the next iteration.)

I have this template function for computing standard deviation in one pass over the data. The underlying math is here.

coderodde_sd.h:

#ifndef CODERODDE_SD_H
#define CODERODDE_SD_H

#include &lt;cmath&gt;
#include &lt;iterator&gt;
#include &lt;sstream&gt;
#include &lt;stdexcept&gt;

namespace net {

    namespace coderodde {

        namespace stat {

            template&lt;class Iter&gt;
            double sd(Iter begin, Iter end)
            {
                typedef typename std::iterator_traits&lt;Iter&gt;::difference_type 
                                                             difference_type;

                difference_type distance = std::distance(begin, end);

                if (distance &lt; 2)
                {
                    std::stringstream ss;
                    ss &lt;&lt; "The standard deviation cannot be computed for "
                          "less than two elements. The input sequence has "
                       &lt;&lt; distance 
                       &lt;&lt; " elements.";

                    throw std::runtime_error(ss.str());
                }

                typedef typename std::iterator_traits&lt;Iter&gt;::value_type 
                                                             value_type;

                double x = 0.0;
                double x_squared = 0.0;

                for (Iter it = begin; it != end; ++it) 
                {
                    x += *it;
                    x_squared += (*it) * (*it);
                }

                return std::sqrt((x_squared - (x * x) / distance) / 
                                 (distance - 1)
                                );
            }

        } /* net::coderodde::stat */

    } /* net::coderodde */

} /* net */

#endif  /* CODERODDE_SD_H */


main.cpp:

#include &lt;iostream&gt;
#include &lt;list&gt;
#include "coderodde_sd.h"

using net::coderodde::stat::sd;
using std::cout;
using std::list;

int main(int argc, char** argv) {
    double bad_array[]{1.0};

    try 
    {
        sd(bad_array, bad_array);
    }
    catch (std::runtime_error&amp; error)
    {
        cout &lt;&lt; "ERROR: " &lt;&lt; error.what() &lt;&lt; "\n";
    }

    try 
    {
        sd(bad_array, bad_array + 1);
    }
    catch (std::runtime_error&amp; error)
    {
        cout &lt;&lt; "ERROR: " &lt;&lt; error.what() &lt;&lt; "\n";
    }

    std::list&lt;int&gt; my_list = { 1, 5, 2, 4, 3 };

    cout &lt;&lt; "Standard deviation: " 
         &lt;&lt; sd(my_list.begin(), my_list.end())
         &lt;&lt; "\n";

    return 0;
}


I tried hard to write idiomatic C++; please tell me where I failed to reach that ideal + any other critique.
In your example, you have two passes over the list - std::distance works in linear time unless it operates on random access iterators.

A naive way to fix this is to calculate the number of elements inside your for loop and check it afterwards. But this way you'll pessimize performance for random access iterators.

To avoid this pessimization you can examine the iterator_tag of you Iter at compile time and decide whether you're going to call std::distance or count your elements in a loop.

The simplest way to do this is in C++14 is by tag dispatching. Here is an example:

template&lt;typename RandomAccessIter&gt;
double sd(RandomAccessIter begin, RandomAccessIter end, std::random_access_iterator_tag)
{
    // calling std::distance
}

template&lt;typename ForwardIter&gt;
double sd(ForwardIter begin, ForwardIter end, std::forward_iterator_tag)
{
    // counting elements in a loop
}

template&lt;typename Iter&gt;
double sd(Iter begin, Iter end)
{
    // dispatching
    return sd(begin, end, typename std::iterator_traits&lt;Iter&gt;::iterator_category());
}


This code will corretly handle bidirectional iterators as well since std::bidirectional_iterator_tag inherits from std::forward_iterator_tag. And code duplication can be avoided by incaplusating common logic in separate functions.

Viola! Now you have only one pass.