Counting inversions with merge sort I am piggybacking an iterator-based implementation of merge-sort to count array inversions. My first correct solution looks as follows:

#include &lt;vector&gt;
#include &lt;iterator&gt;
#include &lt;stdio.h&gt;

template&lt;typename It&gt;
std::vector&lt;typename It::value_type&gt; SortAndCountInversionsSubroutine(const It begin, const It middle, const It end, unsigned int&amp; count)
{
    std::vector&lt;typename It::value_type&gt; v;
    It left{ begin }, right{ middle };
    while (left != middle &amp;&amp; right != end)
    {
        if (*left &lt;= *right)
        {
            v.push_back(*left++);
        }
        else
        {
            count+= std::distance(left, middle);
            v.push_back(*right++);
        }
    }
    v.insert(v.end(), left, middle);
    v.insert(v.end(), right, end);
    return v;
}

template&lt;typename It&gt;
void SortAndCountInversions(It begin, It end,unsigned int&amp; count)
{ 
    auto size = std::distance(begin, end);
    if (size &lt; 2)
        return;
    auto mid = std::next(begin, size / 2);
    SortAndCountInversions(begin, mid, count);
    SortAndCountInversions(mid, end, count);
    auto v = SortAndCountInversionsSubroutine(begin, mid, end, count);
    std::move(v.cbegin(), v.cend(), begin);
}


The implementation can be used as follows:

std::vector&lt;int&gt; v1{ 1, 3, 5, 2, 4, 6 };
unsigned int inversionCount = 0;
unsigned int expectedCount = 3;
SortAndCountInversions(v1.begin(), v1.end(), inversionCount);
Assert::AreEqual(expectedCount, inversionCount);

inversionCount = 0;
expectedCount = 5;
std::vector&lt;int&gt; v2{ 1, 20, 6, 4, 5 };
SortAndCountInversions(v2.begin(), v2.end() , inversionCount = 0);
Assert::AreEqual(expectedCount, inversionCount);


I am looking for any suggestions to improve my code. Particularly, I would like to return the inversion count as a return value, instead of a parameter reference. Any ideas?
Looks nice and clean.

One thing to note:

With Iterators Const Iterator is not the same as Iterator_Const. The first means you will not change the iterator while the second means you will not change what the iterator points at. You actually want to mean the second the first one is rarely useful.

&lt;ReturnType&gt; SortAndCountInversionsSubroutine(
                 const It begin, const It middle, const It end,
            //   ^^^^^           ^^^^^             ^^^^
                 unsigned int&amp; count)


That's not a very useful const.
Just use:

template&lt;typename It&gt;
// When C++ concepts get added just uncomment the line below.
// requires std::is_const&lt;typename std::iterator_traits&lt;It&gt;::reference_type&gt;::value
&lt;ReturnType&gt; SortAndCountInversionsSubroutine(
                 It begin, It middle, It end,
                 unsigned int&amp; count)


I "might" change this:

auto v = SortAndCountInversionsSubroutine(begin, mid, end, count);
std::move(v.cbegin(), v.cend(), begin);


I might move the std::move() into the SortAndCountInversionsSubroutine() the trouble with that is you need to change the iterators from being iterator_const (once you have fixed the first point) to non const. The advantage of course is that you don't have to return a vectorfrom a function.